0

I want to convert json string to json object with it's order. My code:

String responseData = "{ \"contents\": [ { \"a\" : \"A\" , \"b\": \"B\", \"c\" : \"C\" } ] , \"maxsize\" : 5, \"startIndex\" : 1, \"status\" : \"suspended\", \"activated\" : false}"
JSONObject jsonObject = new JSONObject(responseData);
System.out.println(jsonObject.toString());

I used org.json version - 20200518 library and when I tried , I got following output:

{"startIndex":1,"contents":[{"a":"A","b":"B","c":"C"}],"maxsize":5,"status":"suspended","activated":false}

but I want below output:

{ "contents" : [ { "a" : "A" , "b": "B", "c" : "C" } ] , "maxsize" : 5, "startIndex" : 1, "status" : "suspended", "activated" : false}

Any help is much appreciated!

Amit Gawali
  • 270
  • 2
  • 4
  • 18
  • A side note: `{ "contents": [ "a" : "A" , "b": "B", "c" : "C"] , "maxsize" : 5, "startIndex" : 1, "status" : "suspended", "activated" : false}` your input json cannot be parsed. Please update your code. – gtiwari333 Sep 30 '20 at 14:19
  • 1
    @gtiwari333 Code updated.. – Amit Gawali Sep 30 '20 at 14:22
  • 1
    org.json uses HashMap to store the parsed data. so you cannot guarantee the order. Simple solution to this is : If you can change your json library then use fasterxml-jackson. It uses LinkedHashMap and preserves the order. – gtiwari333 Sep 30 '20 at 14:27
  • Thank you @gtiwari333. It solves my problem. Is this library Open source? – Amit Gawali Sep 30 '20 at 14:41
  • Yes it is opensource https://github.com/FasterXML/jackson – gtiwari333 Sep 30 '20 at 14:43

2 Answers2

1

org.json uses HashMap to store the parsed data. So you cannot guarantee the order.

Ideally ordering of the elements should not matter. But if you must need that and can change your json library then use fasterxml-jackson. It uses LinkedHashMap and preserves the order.

Also note that org.json is very basic + lightweight(66KB) library that does the job. Jackson is at least 1.7MB+.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

public class JsonTest {

    String responseData = "{ \"contents\" : [ { \"a\" : \"A\" , \"b\": \"B\", \"c\" : \"C\" } ] , \"maxsize\" : 5, \"startIndex\" : 1, \"status\" : \"suspended\", \"activated\" : false}";

    @Test
    void orgJson() {
        JSONObject jsonObject = new JSONObject(responseData);
        System.out.println(jsonObject.toString()); //no ordering
    }

    @Test
    void jackson() throws JsonProcessingException {
        JsonNode jsonObject = new ObjectMapper().readTree(responseData);
        System.out.println(jsonObject.toString()); //keeps the ordering
    }
}
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
0

A JSONObject is not inherently an ordered object. So unfortunately what you require is not possible with how the JSONObject class works in Java. The best way for you to sort would be to pass an index within your object that would indicate sort and then use it as such. Here is an article outlining how that can be accomplished.

https://www.tutorialspoint.com/how-can-we-sort-a-jsonobject-in-java

Nathan Walsh
  • 358
  • 2
  • 4