Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
NNayal1600240775
Contributor III
Contributor III

JSONObject incorrect ordering

This is my tjavarow:

JSONObject finalJson = new JSONObject();

finalJson.put("A", input_row.A);

finalJson.put("BId", input_row.BId);

finalJson.put("status", input_row.status);

globalMap.put("finalJson", finalJson.toString());

System.out.println("finalJson: " + finalJson.toString());

But my tlog row is :

{"BId":"AB123","A":"","status":"Approved"}

But instead it should have looked like:

{"A":"","BId":"AB123","status":"Approved"} --How can I achieve this. The log row iam getting doesnt have correct ordering

Labels (5)
1 Solution

Accepted Solutions
anselmopeixoto
Partner - Creator III
Partner - Creator III

Hi @Neha Nayal​ 

 

Since a JSON object is described as "an unordered set of name/value pairs" (https://www.json.org/json-en.html), this is an expected behavior from a JSON library.

 

However, if you need to have an ordered JSON, it seems you can achieve it using a LinkedHashMap object or JsonObject class from com.google.gson.

 

Please take a look at this for reference: https://stackoverflow.com/questions/3948206/json-order-mixed-up

View solution in original post

2 Replies
anselmopeixoto
Partner - Creator III
Partner - Creator III

Hi @Neha Nayal​ 

 

Since a JSON object is described as "an unordered set of name/value pairs" (https://www.json.org/json-en.html), this is an expected behavior from a JSON library.

 

However, if you need to have an ordered JSON, it seems you can achieve it using a LinkedHashMap object or JsonObject class from com.google.gson.

 

Please take a look at this for reference: https://stackoverflow.com/questions/3948206/json-order-mixed-up

jlolling
Creator III
Creator III

The order of the attributes are irrelevant! The component suite: tJSONDoc* (https://github.com/jlolling/talendcomp_tJSONDoc) keeps the order of the attributes because the underlaying lib does this. But again, it does not matter!