JSON Structure for List of Objects - java

I would like to know, whats the right structure for a list of objects in JSON.
We are using JAXB to convert the POJO's to JSON.
Here is the choices, Please direct me what is right.
foos: [
foo:{..},
foo:{..}
]
or
foos : [
{...},
{...}
]
If the first structure is right, what is the JAXB annotation I should use to get the structure right.

The second is almost correct:
{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}

The first example from your question,
foos: [
foo: { ... },
foo: { ... }
]
is in invalid syntax. You cannot have object properties inside a plain array.
The second example from your question,
foos: [
{ ... },
{ ... }
]
is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.
Following is the correct one when you want to obey strict JSON:
"foos": [
{ ... },
{ ... }
]
This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.

As others mentioned, Justin's answer was close, but not quite right. I tested this using Visual Studio's "Paste JSON as C# Classes"
{
"foos" : [
{
"prop1":"value1",
"prop2":"value2"
},
{
"prop1":"value3",
"prop2":"value4"
}
]
}

I use jsonformatter page to test the json validator.
try to use jsonwhatever
example:
list_a = func_generator_of_objects()
json_string = jsonwhatever('foos',list_a)

Related

Check two JSON if match and generate report with deferent

I am trying to check if two JSON file is equal or not using java :
This is the first json
{
"filters": [
{
"name": "Data_Type",
"value": "Database"
},
{
"name": "Begin_Date",
"value": "2019-05-01"
},
{
"name": "End_Date",
"value": "2019-10-31"
}
]
}
and this is the secound one :
{
"filters": [
{
"name": "End_Date",
"value": "2019-10-31"
},
{
"name": "Begin_Date",
"value": "2019-05-01"
},
{
"name": "Data_Type",
"value": "Database"
}
]
}
I use this library zjsonpatch
This library is great but the issue for me I want to ignore the order for the array so In my two JSON file should match
Alo I don't need only check the match, also I need report with the defferant if exists as zjsonpatch provide
any suggestion ??
Your problem is relatively simple to solve. I won't write real code because you didn't specify what language you are using so you will get pseudocode.
create a method to determine if a json object is equal to another according to your rules
parse json a
place every individual item of json a into a collection
parse json b
place every individual item of json a into another collection
iterate over collection a removing item by item and trying to removing the current item from collection b
when a item from A can't be removed from B or if when you finish iterating collection B is not empty it means the jsons were different

How to extract a particular object type from json if the level is not pre-known?

I have a json like shown below (this is just representational):
The issue I am facing is that the Person object can be at different levels in the json. E.g. in below case it is at level 2 in case of RootNode1, at level 1 in case of RootNode2 and at level 0. Of course these levels are not limited to 2 and neither are they tied to RootNode in any way. (And these node values ar enot preknown. Only thing fixed and unique to identify Person object is "Type": "Person")
I have to extract Person object in all cases.
Is there a way to achieve this through traversal in JsonPath library : https://github.com/json-path/JsonPath?
[
{
"RootNode1": [
{
"ABC": [
{
"DEF": ""
},
{
"Name": "John",
"Type": "Person"
....
}
]
},
{
"DAC": {}
}
]
},
{
"RootNode2": [
{
"Name": "Williams",
"Type": "Person"
....
}
]
},
{
{
"Name": "Sam",
"Type": "Person"
....
}
}
]
Yes, it is completely possible.
If the readme is to be believed, he make
JsonPath.read(document, "$.RootNode1.ABC[1].Type");
For RootNode1
And
JsonPath.read(document, "$.RootNode2[1].Type");
For RootNode2
And
JsonPath.read(document, "$.Type");
it is the only way to do that I see. but there may be simpler, I'm used to using org.json personally
If I understand you correctly, then this jsonpath expression
$..Type
should output
[
"Person",
"Person",
"Person"
]
at whatever level Type is.
The correct way to do that is with the expression: $..[?(#.Type == 'Person')], like:
ptx.parse(json).read("$..[?(#.Type == 'Person')]", List.class)
// ptx is ParseContext object of com.jayway.jsonpath
With this I can get the List of persons without caring about the level the Person object was present.

How to map two different responses from an REST API to two different POJO

I have a use case where a service returns two different responses for the same REST API call. Only the response value is different, but the key is the same. Please refer to the sample responses below,
Response 1
{
"Polygon": {
"type": "polygon",
"coordinates": [
[
[
[
-128.419935,
31.761895
],
[
88.53,
87.24
]
]
]
]
}
}
Response 2
{
"Polygon": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-118.419935,
33.761895
],
[
89.53,
86.24
]
]
]
]
}
}
If we read the response carefully, then we can understand that the two responses but differ only by value (i.e. coordinates). The coordinates are the two different nested levels of List. So I'm not sure how should I map the response. I'm using Jackson for mapping the response.
I did have a look at this SO question, but that addresses if the property values are different. Can someone through some lights on it.
Thanks, #Lino. Your suggestion helped me to figure out a solution to my problem. I'm from the C++ background but working with JAVA now. I realised the Object is the root of the class hierarchy i.e. its superclass for all the class. So my solution to the above problem would like as below,
public class Polygon {
private data boundingPolygon;
public static class data {
private String type;
private Object coordinates;
}
}
I have tested this and it works for me.

Converting JSON Array to Java Object when Array Elements Could Be Different

Could anyone offer suggestions on a JSON library or code snippet that deals with converting a JSON file such as the one below to a Java object when there could be different key-value pairs in each array element?
{
"tweets": [
{
"filter_level": "medium",
"retweeted_status": {
"contributors": null,
"text": "",
"entities": {
"symbols": [
],
"urls": [
],
"hashtags": [
],
"user_mentions": [
]
},
"more data":". . ."
"user": {
"user data":". . .",
. . .
},
},
"contributors": null,
"text": "",
"entities": {
"symbols": [
],
"urls": [
],
"hashtags": [
],
"user_mentions": [
{
"id": 32943506,
"name": "Jazzmen",
"indices": [
3,
17
],
"screen_name": "_PumpsAndJays",
"id_str": "32943506"
}
]
},
"more data":". . .",
"user": {
"user data":". . .",
. . .
},
},
{ //BEGIN NEW ELEMENT, As you can see this next element doesn't have the
"retweeted_status" key, so it's different from the element of the array before.
"filter_level": "medium",
"contributors": null,
"text": "",
"more data like the last element":". . .", . . .
I would like to convert a json object like this, made up of an array of "tweets" with information for each, but I cannot find any documentation on a case when the data differs slightly from element to element. In this specific case, an element may be a retweet or just a normal tweet. I would like to know if anyone has insight on how to do this conversion using a library like Jackson, Google Gson, or the like?
I have done a TON of research on this, read multiple tutorials, the documentation for multiple parser libraries and cannot find anything like this.
The quickest way to do this is to have the following class:
class Tweets {
public List<Map<String, Object> tweets;
}
Using Jackson and passing this class as the desired output will deserialize the JSON without errors, but you need to know the exact name of the fields for each tweet so that you can do .get("field_name") for them. You will also need to cast the value to what you know it to be: String, Integer, Map for nested objects, etc. This code will be hard to maintain.
Another option is to have a Tweet class that has all the possible values you expect from all variations on the tweet objects received. So add retweetedStatus even if it might not have it for certain objects in the array. Jackson will just leave the value as null if it's not present in the input.
class Tweets {
public List<Tweet> tweets;
}
class Tweet {
String filterLevel;
RetweetedStatus retweetedStatus;
String text;
// + any other fields you may receive and want to deserialize
}
// + any other classes the define nested objects like RetweetedStatus
You can even configure Jackson to ignore fields that are not mapped in your class if you want to ignore any fields in the json.
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
or by annotating the class
#JsonIgnoreProperties(ignoreUnknown = true)
public class MyDtoIgnoreUnknown { ... }
You can also use annotations to specify a different name for the variable than the one in the json:
#JsonProperty("retweeted_status")
public RetweetedStatus retweetedStatus
Notice the change from underscore separated to camelCase

How do I use xStream to output Java Objects with List properties?

I am trying to output some Java objects as JSON, they have List properties which I want to be formatted as { "People" : [ { "Name" : "Bob" } , { "Name" : "Jim" } ] }
However, I cannot figure out how to do this with XStream. It always outputs as { "Person" : { "Name" : "Bob" }, "Person" : { "Name" : "Bob" }
Is there a way to fix this? I've put together some sample code with a unit test in github if you need something more concrete to play with: http://gist.github.com/371358
Thanks!
I assume that the problem is with #XStreamImplicit which is declared on top of the List, remove that and try. It should work fine ;)
/jay

Categories

Resources