Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON order mixed up

Tags:

java

json

I've a problem trying to make my page printing out the JSONObject in the order i want. In my code, I entered this:

JSONObject myObject = new JSONObject(); myObject.put("userid", "User 1"); myObject.put("amount", "24.23"); myObject.put("success", "NO"); 

However, when I see the display on my page, it gives:

JSON formatted string: [{"success":"NO", "userid":"User 1", "bid":24.23}]

I need it in the order of userid, amount, then success. Already tried re-ordering in the code, but to no avail. I've also tried .append....need some help here thanks!!

like image 204
JSON guy Avatar asked Oct 16 '10 08:10

JSON guy


People also ask

Does order matter in JSON?

The JSON RFC (RFC 4627) says that order of object members does not matter.

How do I change the order of JSON?

You cannot change the order if you are using the JSON function. It will always be alphabetical.

Does JSON maintain order?

Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine): An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

Are JSON object fields ordered?

The JSON Data Interchange Standard definition at json.org specifies that “An object is an unordered [emphasis mine] set of name/value pairs”, whereas an array is an “ordered collection of values”. In other words, by definition the order of the key/value pairs within JSON objects simply does not, and should not, matter.


2 Answers

You cannot and should not rely on the ordering of elements within a JSON object.

From the JSON specification at https://www.json.org/

An object is an unordered set of name/value pairs

As a consequence, JSON libraries are free to rearrange the order of the elements as they see fit. This is not a bug.

like image 51
Adrian Smith Avatar answered Sep 17 '22 17:09

Adrian Smith


I agree with the other answers. You cannot rely on the ordering of JSON elements.

However if we need to have an ordered JSON, one solution might be to prepare a LinkedHashMap object with elements and convert it to JSONObject.

@Test def void testOrdered() {     Map obj = new LinkedHashMap()     obj.put("a", "foo1")     obj.put("b", new Integer(100))     obj.put("c", new Double(1000.21))     obj.put("d", new Boolean(true))     obj.put("e", "foo2")     obj.put("f", "foo3")     obj.put("g", "foo4")     obj.put("h", "foo5")     obj.put("x", null)      JSONObject json = (JSONObject) obj     logger.info("Ordered Json : %s", json.toString())      String expectedJsonString = """{"a":"foo1","b":100,"c":1000.21,"d":true,"e":"foo2","f":"foo3","g":"foo4","h":"foo5"}"""     assertEquals(expectedJsonString, json.toString())     JSONAssert.assertEquals(JSONSerializer.toJSON(expectedJsonString), json) } 

Normally the order is not preserved as below.

@Test def void testUnordered() {     Map obj = new HashMap()     obj.put("a", "foo1")     obj.put("b", new Integer(100))     obj.put("c", new Double(1000.21))     obj.put("d", new Boolean(true))     obj.put("e", "foo2")     obj.put("f", "foo3")     obj.put("g", "foo4")     obj.put("h", "foo5")     obj.put("x", null)      JSONObject json = (JSONObject) obj     logger.info("Unordered Json : %s", json.toString(3, 3))      String unexpectedJsonString = """{"a":"foo1","b":100,"c":1000.21,"d":true,"e":"foo2","f":"foo3","g":"foo4","h":"foo5"}"""      // string representation of json objects are different     assertFalse(unexpectedJsonString.equals(json.toString()))     // json objects are equal     JSONAssert.assertEquals(JSONSerializer.toJSON(unexpectedJsonString), json) } 

You may check my post too: http://www.flyingtomoon.com/2011/04/preserving-order-in-json.html

like image 24
lemiorhan Avatar answered Sep 18 '22 17:09

lemiorhan