Using jackson to traverse json tree - java

I'm developing a new system that talks to a third party via JSON.
One of the calls returns a huge JSON structure to represent products and rules.
I've used Jackson to convert this JSON into a tree quite easily. Now the issue is I want to be able to find nodes by 'querying' without manually traversing the whole tree.
So somewhere deep in the tree is an object which has a field called business_id. I want to return all the nodes that have this field.
Is that possible?

You can use Jackson's JsonNode class documented here:
http://fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/JsonNode.html
Parse your data into a JsonNode (e.g. by ObjectMapper.readValue), then you can traverse programmatically that JSON structure as a tree.
Look at methods like: as{datatype}, find[Value|Values], is[Array|Object|{datatype}], path etc.

You could try Json Path, it lets you pick up a json node using it's xpath:
http://code.google.com/p/json-path/

Related

jackson-core: Dependent pair, where type family exists before quantifier in Json data

I have some incoming JSON (the field-order of which is not my choice) that embeds a dependent pair:
{
"data": {...},
"evt": "READY",
...
}
and what type I should read data into depends on the value of evt. With just a JsonParser this is impossible because there's no way to store data for later so that it can be returned to once evt is reached.
All of the data I'm parsing (unfortunately) already exists in a ByteBuffer, so is there a better interface to use than JsonParser? I don't want to bring in any more dependencies than jackson-core if it can be helped.
Looks like there is no simple way to achieve this without any additional dependencies.
I suppose, you need to add at least jackson-databind (and also jackson-annotations if not added automatically via Maven/Gradle).
Then you can use an ObjectMapper as an ObjectCodec for the parser and parse the complete JSON either into a TreeNode structure that can be partically parsed later into the correct type or - if you have objects for all types of data - you maybe can directly parse the complete object with matching data type. If needed, a custom ObjectCodec could be implemented to first collect the unknown data and then later process it when the type is known, but implementing an ObjectCode does not seem to be that easy.
Instead of Jackson you could use GSON which can either parse the data into the complete object structure or a generic JSON object tree without any additional dependencies.
If you really cannot add additional dependencies, then you could implement a SAX-XML-Parser-like logic using JsonParser.nextToken, but I suppose that would require a lot of custom logic.

How to convert efficiently a Java object to SoyData in Google Closure

We have a Java object with data that we want to use for template rendering - is there an efficient way of converting Java object to SoyData?
Currently we are using gson to convert it to tree and recursively populate SoyData object, but maybe there is something that wouldn't require gson conversion.
So in the end I added a constraint that the data can be only in a form of a map and I was able to use a library object com.google.template.soy.data.SoyMapData to do the work.

How to extract nested JSON Elements in Liferay

I'm using Liferay 6.2 and have a JSONObject containing the following:
{
"foo":{
"bar":{
"baz":["42","23"]
}
}
}
I have a String containing a path/selector/whatsitcalled pointing somewhere in that JSONObject: foo.bar.baz[0]
How would I go about getting the corresponding value from the JSONObject, i.e. "42"?
All methods I could find only deal with the next level down, nothing seems to cover nesting. I could parse the path manually, but isn't there an easier way?
Use this java library to perform xpath similar query for json.
I've used it with success in Liferay.
https://github.com/jayway/JsonPath
There are many ways to retrive data programatically from JSON object.
You can use the own Liferay support JSON library, GSON or Jackson libraries ( I recommend you the last one, it is really powerful).

Efficiently find a key-value in JSON with JAVA

I want to find whether a key-value pair exists in a JSON given a key. This key-value pair may not be under root. It's possible embedded deeply in the document. For example:
{"Persons":[
{"Person":
{"name":"john"}
}]
}
and I want to have something like JsonDoc.has("name"), and return true.
I search online and find this thread: Java: Json has key/field
Two answers (minimal-json and org.json) both has some functioned called has() or get(), but I looked into their source code, they are both trying to find the key-value under root. So they don't meet my needs.
I can think of traversing the whole json and try to find the key-value, but it seems not efficient.
If you use Jackson, you can call findParent on a JsonNode which will look for a field within the node or its descendant.
http://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/JsonNode.html#findParent(java.lang.String)

json schema containing multiple independent types

Is there any example to show how a json schema can be written to include multiple independent objects in one file?
If its possible, is there any java library that can convert all of them to individual pojos and vice versa?
JSON requires you to always have either one array or one object as root element. If that is not the case, it is not valid JSON.
That being said, why don't you just wrap your multiple indidpendent objects in one root object? Doing this, you will be able to use any functional JSON marshaller.

Categories