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!

5
  • 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. Commented Sep 30, 2020 at 14:19
  • 1
    @gtiwari333 Code updated.. Commented Sep 30, 2020 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. Commented Sep 30, 2020 at 14:27
  • Thank you @gtiwari333. It solves my problem. Is this library Open source? Commented Sep 30, 2020 at 14:41
  • Yes it is opensource github.com/FasterXML/jackson Commented Sep 30, 2020 at 14:43

2 Answers 2

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.