Inverted order of JSON elements in Java after XML conversion - java

I'm using the JSON in Java for the transformation of XML to JSON. I have the problem that this implementation is inverting all child elements.
When I pass this XML:
<Person><Child1>a</Child1><Child2>b</Child2></Person>
I will end up with a JSON having the childs inverted:
{"Person":{"Child2":"b", "Child1":"a"}}
My Java code:
JSONObject jsonObject= XML.toJSONObject("<Person><Child1>a</Child1><Child2>b</Child2></Person>");
String myJSONString = jsonObject.toString(4);
How to transform to JSON with keeping the order of the elements (like in XML)?

So my question. How to transform to JSON with keeping the order?
With the current official JSONObject, this is not possible. The API makes it very clear:
A JSONObject is an unordered collection of name/value pairs.
But, there might be a quick workaround for your problem. As from what I've investigated in the JSONObject source code, you can see that it uses a HashMap internally, and as you know HashMap doesn't keep any order.
public JSONObject() {
this.map = new HashMap<String, Object>();
}
You have 2 alternatives:
Modify the current JSONObject source code so that the map is initialized with a LinkedHashMap. A LinkedHashMap is an implementation of the Map interface, with predictable iteration order:
public JSONObject() {
this.map = new LinkedHashMap<String, Object>();
}
Make your own custom class which extends JSONObject but uses a LinkedHashMap internally. Notice that you still have to make some changes in JSONObject.
public class JSONObject {
//private final Map<String,Object> map; // current approach
//you have to remove final modifier and either add a getter or make it protected. I'll choose the change modifier to protected in this case.
protected Map<String,Object> map;
}
public class JSONObjectOrdered extends JSONObject {
public JSONObjectOrdered(){
this.map = new LinkedHashMap <String, Object>();
}
}

As JSONObject is an unordered collection of name/value pairs, no choice, you have to use a JSONArray.
Here is my solution, modify the XML class, particularly the method parse, in order to return JSONArray for storing child nodes.
My modified class : XML.java
XML input
<Person name="test"><Child1>a</Child1><Child2>b</Child2><Child3></Child3></Person>
Usage :
JSONObject jsonObject= XML.toJSONObject("<Person name=\"test\"><Child1>a</Child1><Child2>b</Child2><Child3></Child3></Person>");
System.out.println(jsonObject);
Out :
{"Person":{"CHILDREN":[{"Child1":"a"},{"Child2":"b"},{"Child3":""}],"name":"test"}}
Conclusion
The children order is kept. Off course this idea can be improved, it's just a POC regarding what can be done, modifying the parser.

JSON objects don't have a specific order. You can of course change the serialization implementation to keep an order but there is no guarantee that it is also kept after deserialization. In fact, most JSON libraries won't even have an API to detect in which order the original JSON text was parsed. You shouldn't care about ordering when using objects.
If you do care about the order though, use a JSON array.
{"Person":[{"Child1":"a"},{"Child2":"b"}]}

The JSONObject API dose not guarantee the elements order
A nice solution to this issue can be using JSONArray, in JSONArray the order you insert the elements is saved.
So, in your case you will have an array of "chides" for each person.
you would probably will need to change the XML file or manually parse the XML into the json in your format (the JSONArray instead of what you are using now)

If you are hell bent on getting the output ordered the way you want it you could always try overriding the method
toString()

You can download the source code from http://www.json.org/java/ and modify JSONObject.java using TreeMap instead of HashMap.
You also can override method in JSONObject.java
public Iterator<String> keys() {
return this.keySet().iterator();
}
Make sure the Iterator is the one of the sorted keys.

If you would use Jackson for JSON serialization / deserialization you could simply put a
#JsonPropertyOrder() annotation on top of your class.
#JsonPropertyOrder({"Child1", "Child2"})
public class Person {
#JsonProperty("Child1")
public String child1;
#JsonProperty("Child2")
public String child2;
}

You can keep order of incoming data when modify
private final Map<String, Object> nameValuePairs;
/**
* Creates a {#code JSONObject} with no name/value mappings.
*/
public JSONObject() {
nameValuePairs = new HashMap<String, Object>();
}
to
private final Map<String, Object> nameValuePairs;
/**
* Creates a {#code JSONObject} with no name/value mappings.
*/
public JSONObject() {
nameValuePairs = new LinkedHashMap<String, Object>();
}
Because instead of HashMap - LinkedHashMap have an predictable iteration order.
LinkedHashMap : Hash table and linked list implementation of the Map interface, with predictable iteration order.
So is the most effective way to resolve your problem.
And also you can fork to use a custom library from
https://github.com/SergeyShustikov/JSON-java

Related

org.json.simple JSONObject order in which items are inserted

I have an application which passes json data to a third-party application. The third party app requires that the fields in a JSONObject passed to it be in a certain order, or it returns an error "bad request".
My java code reads, in part:
import org.json.simple.JSONObject;
public void postThirdParty (String name, String info, long offset, JSONArray address)
{
JSONObject obj = new JSONObject ();
obj.put ("name", name);
obj.put ("info", info);
obj.put ("offset", offset);
obj.put ("address", address);
system.out.println (obj.toString ());
}
The output is:
{"name":"My Name","info":"My Info","address":[{"addressLine1":"My House No","addressLine2":"My Street","addressTown":"My Town","addressPostCode":"My Postcode"}],"offset":"My offset"}
Any idea why the fourth item to go into the JSONObject is being inserted ahead of the third?
JSONObject does not preserve the order in which the elements were inserted. The underlying implementation of most JSONObjects is a HashMap (which does not preserve the order).
From the docs:
A JSON object. Key value pairs are unordered. JSONObject supports
java.util.Map interface.
Not recommended, but if you are still looking for ordered keys in a HashMap, you can override the .put(...) and initialize the internal Map with a LinkedHashMap instead of a HashMap which will preserve the order.

Mapping JSON with varying object name

I'm quite new to JSON, and I've looked around trying to work out what to do but not sure if I fully understand. I am making an external API call returning:
2015-12-21 01:22:09 INFO RiotURLSender:60 - Total json:
{"USERNAME":{"profileIconId":984,"revisionDate":1450655430000,"name":"USERNAME2","id":38584682,"summonerLevel":30}}
Where 'USERNAME' (And USERNAME2 - which can be very slightly different to USERNAME) will vary depending on what you pass the call's parameters. I was using Jackson Object Mapper to map the individual values within the USERNAME object - but didn't realise I had to map the object as well.
I've been using annotations in the DTOs like:
#JsonProperty("profileIconId")
private Long profileIconId;
and mapping using:
summonerRankedInfoDTO = mapper.readValue(jsonString, SummonerRankedInfoDTO.class);
How do I map using a value of USERNAME which is changing every single time?
Also this seems a bit odd, is this bad practice to have the actual varying key rather than just have the same key and different value?
Thanks
you can use following mentioned annotation #JsonAnyGetter And #JsonAnySetter.
Add this code into ur domain class. So any non-mapped attribute will get populated into "nonMappedAttributes" map while serializing and deserializing the Object.
#JsonIgnore
protected Map<String, Object> nonMappedAttributes;
#JsonAnyGetter
public Map<String, Object> getNonMappedAttributes() {
return nonMappedAttributes;
}
#JsonAnySetter
public void setNonMappedAttributes(String key, Object value) {
if (nonMappedAttributes == null) {
nonMappedAttributes = new HashMap<String, Object>();
}
if (key != null) {
if (value != null) {
nonMappedAttributes.put(key, value);
} else {
nonMappedAttributes.remove(key);
}
}
}
You should try to keep the keys the exact same if possible and change values, otherwise you'll have to change your JSON. Since JSON returns a value from the key, the value can change to anything it wants, but you'll be able to return it from the key. This doesn't work the other way around though.
Anyway to your question, you may have a little better luck using something like the GSON library, its pretty simple to use.
You can create the instance and pass it the JSON string:
Gson gson = new Gson();
JsonObject obj = gson.fromJson(JSON_DOCUMENT, JsonObject.class);
Then you can get certain elements from that now parsed JSON object.
For example, in your JSON string, username returns another JSON element, so you can do:
JsonObject username = obj.get("USERNAME").getAsJsonObject();
Then just repeat the same steps from there to get whatever value you need.
So to get the name which returns "USERNAME2":
username.get("name").getAsString();
Coming together with:
JsonObject obj = gson.fromJson(JSON_DOCUMENT, JsonObject.class);
JsonObject username = obj.get("USERNAME").getAsJsonObject();
username.get("name").getAsString();

Parsing dynamic JSON values to Java objects

In my application I have lot of overviews (tables) with sorting and filtering capabilities. And becuase the different column can hold different value type (strings, numbers, dates, sets, etc.) the filter for these columns also can bring different values. Let me show you few examples (converted to JSON already as is sent to server via REST request):
For simple string value it is like:
{"<column_name>":"<value>"}
For number and date column the filter looks like:
{"<column_name>":[{"operator":"eq","value":"<value>"}]}
{"<column_name>":[{"operator":"eq","value":"<value1>"},{"operator":"gt","value":"<value2>"}]}
For set the filter looks like
{"<column_name>":["<value1>","<value2>"(,...)]}
Now I need to parse that JSON within a helper class that will build the WHERE clause of SQL query. In PHP this is not a problem as I can call json_decode and then simply check whether some value is array, string or whatever else... But how to do this simply in Java?
So far I am using Spring's JsonJsonParser (I didn't find any visible difference between different parsers coming with Spring like Jackson, Gson and others).
I was thinking about creating an own data object class with three different constructors or having three data object classes for all of the three possibilities, but yet I have no clue how to deal with the value returned for column_name after the JSON is parsed by parser...
Simply looking on the examples it gives me three possibilities:
Map<String, String>
Map<String, Map<String, String>>
Map<String, String[]>
Any idea or clue?
Jackson's ObjectMapper treeToValue should be able to help you.
http://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html#treeToValue%28com.fasterxml.jackson.core.TreeNode,%20java.lang.Class%29
Your main problem is that the first version of you JSON is not the same construction than the two others. Picking the two others you could deserialize your JSON into a Map<String, Map<String, String> as you said but the first version fits a Map.
There are a couple solutions available to you :
You change the JSON format to always match the Map<String, Map<String, String> pattern
You first parse the JSON into a JsonNode, check the type of the value and deserialize the whole thing into the proper Map pattern.
(quick and dirty) You don't change the JSON, but you try with one of the Map patterns, catch JsonProcessingException, then retry with the other Map pattern
You'll have to check the type of the values in runtime. You can work with a Map<String, Object> or with JsonNode.
Map<String, Object>
JsonParser parser = JsonParserFactory.getJsonParser();
Map<String, Object> map = parser.parseMap(str);
Object filterValue = filter.get("<column_name>");
if (filterValue instanceof String) {
// str is like "{\"<column_name>\":\"<value>\"}"
} else if (filterValue instanceof Collection) {
for (Object arrayValue : (Collection<Object>) filterValue) {
if (arrayValue instanceof String) {
// str is like "{\"<column_name>\":[\"<value1>\",\"<value2>\"]}"
} else if (arrayValue instanceof Map) {
// str is like "{\"<column_name>\":[{\"operator\":\"eq\",\"value\":\"<value>\"}]}"
}
}
}
JsonNode
ObjectMapper mapper = new ObjectMapper();
JsonNode filter = mapper.readTree(str);
JsonNode filterValue = filter.get("<column_name>");
if (filterValue.isTextual()) {
// str is like "{\"<column_name>\":\"<value>\"}"
} else if (filterValue.isArray()) {
for (JsonNode arrayValue : filterValue.elements()) {
if (arrayValue.isTextual()) {
// str is like "{\"<column_name>\":[\"<value1>\",\"<value2>\"]}"
} else if (arrayValue.isObject()) {
// str is like "{\"<column_name>\":[{\"operator\":\"eq\",\"value\":\"<value>\"}]}"
}
}
}

Can't parse JSON array of arrays to LinkedHashMap in Jackson

I' m developing an Android REST client. We use JSON as data exchange format, so I use a Jackson parser. I get different Json responses from the server like simple arrays:
{"user_id":"332","user_role":"1"}
or something else. All these stuff I parse to LinkedHashMap<String, Object> and everything works perfectly but when I got this response from the server:
[ { "user_id":"352",
"user_role":"expert",
"name":"Test 12-18",
"description":"Test" },
{ "user_id":"263",
"user_role":"novice lab",
"name":"Tom's Desk",
"description":"Desk"}
]
I got null: {} after parsing.Here is my code where i use Jackson:
ObjectMapper mapParametersToJSON = new ObjectMapper();
String serverResponseBody = responseFromServer.getBody();
LinkedHashMap<String, Object> resultofOperation = new LinkedHashMap<String,
Object>();
TypeReference<LinkedHashMap<String,Object>> genericTypeReferenceInformation = new
TypeReference<LinkedHashMap<String,Object>>() {};
try {
resultofOperation = mapParametersToJSON.readValue(serverResponseBody,
genericTypeReferenceInformation);
So, why Jackson failed to parse this? How can I fix this?
Others have suggested the problem, but solutions are bit incomplete. If you need to deal with JSON Objects and Arrays, you can either bind to java.lang.Object, check the type:
Object stuff = objectMapper.readValue(json, Object.class);
and you will get either List or Map (specifically, ArrayList or LinkedHashMap, by default; these defaults can be changed).
Or you can do JSON trees with JsonNode:
JsonNode root = objectMapper.readTree(json);
if (root.isObject()) { // JSON Object
} else if (root.isArray()) { ...
}
latter is often more convenient.
One nice thing is that you can still create regular POJOs out of these, for example:
if (root.isObject()) {
MyObject ob = objectMapper.treeToValue(MyObject.class);
}
// or with Object, use objectMapper.convertValue(ob, MyObject.class)
so you can even have different handling for different types; go back and forth different representations.
The first JSON in your question is a map, or an object. The second is an array. You're not parsing an array, you're parsing a map.
You need to do something like this:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});
Almost identical question with answer here.
In JSON the {"key": "value"} is Object and the ["this", "that"] is Array.
So, in case when you're receiving the array of objects you should use something like List<Map<Key, Value>>.
You are facing an error, because [] construction can't be translated into Map reference, only in List or array.
I would recommend do it something in this way:
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String,String>> parsedResult = objectMapper.reader(CollectionType.construct(LinkedList.class, MapType.construct(LinkedHashMap.class, SimpleType.construct(String.class), SimpleType.construct(String.class)))).readValue(serverResponseBody);
//if you need the one result map
Map<String, String> resultMap = new LinkedHashMap<String, String>();
for (Map<String, String> map: parsedResult){
resultMap.putAll(map);
}

When JsonObject's keys are iterated they aren't in the same order as in the response from the server

I have a very large response from server of JSON string. I converted it to JSON object and then get the keys and iterate it.
The problem is that when I iterate it isnt in the same order as in response from server.
Next then I apply another method by adding all the keys in List<String> and the sort it and then get the iterator of that but still it isn't as I required (as in response).
Code example is here:
JSONObject jsonObject = new JSONObject(responseString);
Iterator<String> myIter = jsonObject.keys();
List<String> sortKey = new ArrayList<String>();
while(myIter.hasNext()){
sortKey.add(myIter.next());
}
Collections.sort(sortKey);
The order of the keys of a JSON object is not supposed to be meaningful. If you want a specific order, you should use an array, not an object.
Your Java code sorts the keys alphabetically. There is no way to get the initial ordering of the keys in the object.
Reference 1:
The order of the keys is undefined
Reference 2:
An object is an unordered set of name/value pairs
You can use Sorted map to put keys and values into. Something like this
public static List listFromJsonSorted(JSONObject json) {
if (json == null) return null;
SortedMap map = new TreeMap();
Iterator i = json.keys();
while (i.hasNext()) {
try {
String key = i.next().toString();
JSONObject j = json.getJSONObject(key);
map.put(key, j);
} catch (JSONException e) {
e.printStackTrace();
}
}
return new LinkedList(map.values());
}
I came across this similar problem while working on section in my android app which displays a list of 1024+ websites alphabetically. Since the json traversal was not in sorted order , I just inserted the json values during traversal into a table ( I m using list adapters in my app) and obtained sorted list of websites with cursor.
So if you are saving what you are fetching from server, you can just query your database to sort the values in the order your want.

Categories