Comments (12)
Can you post a test case / test data to show which behaviour you want exactly?
from json-lenses.
https://gist.github.com/analytically/0b3ac6978f209c91d6b8
from json-lenses.
The usercase:
Unfortunately Capsule CRM returns single element arrays without wrapping it in a JSON array []. So it currently fails when it returns a single organisation and/or a single person.
from json-lenses.
from json-lenses.
I added a new lens arrayOrSingletonAsArray
which won't touch an array but will lift each other json value into a singleton array. Could you try the new 0.5.4-SNAPSHOT
version which has the change?
resolvers += "sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots"
libraryDependencies += "net.virtual-void" %% "json-lenses" % "0.5.4-SNAPSHOT"
from json-lenses.
Working, great! Thanks! analytically/capsulecrm-ciscoipphonedir@99f0fd5
from json-lenses.
In my opinion this should better be a config switch to treat all single elements as arrays. Perhaps something you can configure on the JsonParser? Now I'm inserting arrayOrSingletonAsArray
everywhere, which looks half baked.
eg. 'parties / optionalField("organisation") / arrayOrSingletonAsArray / filter(('contacts / 'phone).is[JsValue](_ => true)) / 'id
from json-lenses.
Better example: { json.extract[String]('parties / 'organisation / arrayOrSingletonAsArray / filter('id.is[String](_ == id)) / 'contacts / 'phone / arrayOrSingletonAsArray / element(0) / 'phoneNumber) }
from json-lenses.
Yeah, I've seen your code and there's definitely still lots of stuff you could improve in json-lenses to make it more succinct. That said, the main improvement in your code would come from reusing previously build lenses. That's the big advantage of lenses: they are completely composable and first-class values. You can put
'parties / 'organisation / arrayOrSingletonAsArray
into a variable and reuse it whereever you need it.
In my opinion this should better be a config switch to treat all single elements as arrays.
I don't think that would work because a rule like this would have to be applied everywhere. What you are requesting is that every field value has to be an array. In fact, that would make your use-case much more complicated than it is now because in the normal case you don't expect field values to be an array.
from json-lenses.
Understood. But I wasn't clear; not every field value, but every JSON child object. This issue stems from XML-to-JSON converters that wrap single XML child entities as single JSON child objects, hence not wrapping it in a array. Therefore one can assume this to be the case everywhere for that parse.
from json-lenses.
Even then, it would mean your lens would have to look like this:
'parties / element(0) / 'organisation / filter('id.is[String](_ == id)) / 'contacts / element(0) / 'phone / element(0) / 'phoneNumber)
I don't see this as a major improvement, it just makes something implicit what really is an explicit requirement of your data-model and should be treated explicitly IMO.
from json-lenses.
Ok agreed. I've simplified the code and it looks better indeed.
from json-lenses.
Related Issues (20)
- Support streaming
- json-path parser doesn't accept literal strings containing spaces HOT 3
- Issue in Parsing
- Extracting Seq of Seqs
- Currently incompatible with spray-json 1.3.x HOT 2
- Type conversion doesn't handle explicit nulls HOT 3
- Support field renames and field mappers? HOT 2
- scala & spray version upgrade? HOT 2
- Stackoverflow on bigger jsons HOT 2
- getOrThrow wraps exception in RuntimeException HOT 2
- Access multiple fields HOT 2
- Unable to set missing value using jsonPath expression HOT 1
- Can't find copyright notice HOT 1
- set doesn't work out of the box for nested json that doesn't exist
- Scala 2.12 version? HOT 2
- Support recursive descent JsonPath syntax (and lens) HOT 1
- Jsonpath: support && and || HOT 2
- java.lang.IndexOutOfBoundsException: Too little elements in array HOT 3
- Spray-routing issue? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-lenses.