I have some spring MVC based APIs which produces JSON/ XML which represents API output.
{
"data" :
{
"users": [
{
"id": "001",
"name: "abc1",
"type": {
"id": "P",
"name": "Permanent"
}
},
{
"id": "002",
"name: "xyz",
"type": {
"id": "C",
"name": "Contractor"
}
}
]
}
}
I'm passing a parameter with request as
url?fields=users.id, users.type.id
users.type.id is a sub-node in users node.
users node is an array.
Now, what I want to do is to filter those only properties and create the response based upon fields passed in the request.
So the response to above filter condition should be same structure and will only contain wanted fields with values.
I'm trying to build a flat map with keys with a dot notation so I won't lose the track to filter, then I'll rebuild the JSON again. I feel this approach is just unreasonable because Jackson has the .path and .with API's to check existing path. But the real challenge is to extract and create a new JSON which matches the response JSON.
I'm looking for some ideas to achieve this. I don't want to try any third party libs btw. I know some libs are there. I want to prefer Jackson way to do this.
Feel free to add or comment if you have a further ideas.
Related
I have a nested json structure and a list of json paths. I want to extract and return only those parts of json that are defined in the json paths.
For eg: Let's say the json contains:
{
"id": 1,
"user": {
"name": "ABCD",
"email": "abcd#email.com",
"phone": "1234"
}
}
And I've the following JsonPath's
$.id
$.user.name
$.user.phone
So the json returned will be
{
"id": 1,
"user": {
"name": "ABCD",
"phone": "1234"
}
}
I've tried multiple ways with Jayway's JsonPath but I only get the value, not the entire heirarchy(Eg: If I search for $.user.name then I get ABCD instead of {"user": {"name": "ABCD"}}).
I looked at Extracting a subset of attributes with JSONPath but it does not answer the question of return entire json structure.
Any ideas how do I approach this? I suspect that I would need to build Jackson JsonNode iteratively using the JsonPath, but couldn't build a solution.
Note: The example above contains a simple json structure. In reality, I suspect to have many nested structures(even arrays) inside json.
I have a server that I talk to which I have no control over. I simply consume, and that's it. I get responses from it in the form of JSON. I use GSON to instantiate a Java object from this JSON. Later, I get another response from the server that represents an update that needs to happen to a portion of that object. It could be any number of fields and other member objects that get updated, down through any part of the tree of objects from the main object the JSON represents.
Initially, the object comes in (without formatting) looking something like this:
{
"action": "ephmeral",
"object1": {
"id": 1,
"stuff": [{
"id": 2,
"type": "something",
"where": "there",
"count": 5
}, {
"id" 3,
"type": "otherthing",
"where": "here",
"count": 5
}
],
"field2": 2,
"field3": 3,
"field4": 4,
"field5": 5,
...
"fieldN": 999,
}
}
Which is a small example representing any amount of complexity in the object and its fields. This is the initial state of the main object.
Later on, there is communication to the server to update the object in small way (any number of fields can be updated, this is a small example).
[{
"action": "update",
"object1": {
"id": 1,
"stuff": [{
"id": 2,
"count": 10
}
]
}
}, {
"action": "delete",
"object1": {
"id": 1,
"stuff": [{
"id": 3
}
]
}
}, {
"action": "update",
"object2": {
"id": 5,
"amount": 123.45
}
}
]
Those action tags denote a specific way the object or any of its fields should be updated (well, were updated server side already) in some way. It could be updating things, deleting things, etc. as per the value of action.
My question is, not being familiar with JSON and REST, and some of the other technologies the server uses (it's using Rails for example; I'm using Java), I'm not even sure how to go about searching to see if there is a name to this particular way of doing things that I can then find a library for to integrate into my Java code. Alternatively, is there some way that GSON can handle this properly? As it stands, it gives me a new object representing the first action, and none of the subsequent actions affect the object (I'm not thinking of this as a bug or anything wrong with GSON).
At the moment I'm parsing the JSON response string myself for known, fixed (ish) responses for a few, small, critical requests. But this is, of course, not at all dynamic or flexible. Is this a custom communication style, or is it a known style that I can get handled flexibly and dynamically?
I wanted to create a service which can accept JSON as an input to my endpoint. eg.
[{ "name": "Ram","event": "processed" },
{"name": "Raj", "event": "click" }] .
Tried some of the possible ways, cloud endpoints are not supporting arrays or any array related types to use. I tried it to convert the json to string but it's not working for me.
Thanks in advance.
You seem to have an invalid JSON. Try validating it here. As you can see, you will encounter a parsing error.
Change all occurrences of “ with "
[
{
"name": "Ram",
"event": "processed"
},
{
"name": "Raj",
"event": "click"
}
]
and now you have a valid JSON.
More details on JSON can be found here.
I was given a pojo like below,
{
"id": "yyy",
"$schema": "http://json-schema.org/draft-04/schema#",
"packages":
{
"id": "#/packages",
"description": "XXX",
"type": "object",
"properties":
{
"xx":
{
"description": "Total number of elements in the collection.",
"type": "integer",
"minimum": 1,
"minLength": 1
}
..............
...............
},
"additionalProperties": false,
"required":
[
"xx",
...
...
]
}
}
When trying to generate POJO with the Jsonschema2POJO I just see the package being created with no class files in it. Even no exceptions occur.
When I add a "properties" around the "packages" tag then it generates the pojo but if any other json references this json i get a "Path not present:packages" error and moreover it makes the schema invalid.
I want to understand if there is any limitation with the tool on the schema version? Or is there any modifications that has to be done in the jsons provided to make it work with the tool. Please suggest.
Answering myself with something that worked for me,
schema is hidden inside packages, it cannot be seen by jsonschema2pojo. To use this schema, either put it at the root of the file use a $ref to point to it from somewhere else (like "$ref":"#/packages".
Click https://github.com/joelittlejohn/jsonschema2pojo/issues/366 for further reference.
I have a json string like this:
"files": {
"fileA.c": {
"size": 100
},
"fileB.txt": {
"size": 200
}
}
I want to extract the file names, {"fileA.c","fileB.txt"}, using JsonPath. Note that the number of files is unknown.
The problem is, I don't know whether the file name is a key or a value:
If it is a key...well I certainly don't know the key name because that's the information I want to extract.
If it is a value, then what is its key?
Can I use JsonPath to extract the file names? If so, how?
If JsonPath cannot do this, is there any Java library for Json that can achieve this?
In your example, fileA.c and fileB.txt are keys, you can get them by iterating on the key in the enclosing object (which is referenced by the key "files").
I don't think JSONPath is really appropriate (or even applicable) in this case, it is designed to access elements when you know the structure of the documents, which means that basically you know the keys. It would be much easier to simply use a JSON parser.
I would suggest you to modify your data structure, to something like this;
var data = {
"files": [
{
"name": "fileA.c",
"size": 100
},
{
"name": "fileB.txt",
"size": 200
},
{
"name": "fileC.txt",
"size": 50
}
]
};
When structured like this, you can use DefiantJS (http://defiantjs.com) to query for files which sizes are larger than 100...like this:
JSON.search(data, '//*[size >= 100]/name')
DefiantJS extends the global object JSON with the method "search", with which you can query JSON structure with XPath expressions.
To see a working example with your data, check out this fiddle:
http://jsfiddle.net/jRN22/