jhunjhunwala
jhunjhunwala

Reputation: 5

Why does Json object comes before Json values in below?

I am trying to create nested json object using org.json.simple.JSONObject. why does jsonobject change order?

Expected output:

{  
  "id":"14",   
  "email":"[email protected]",    
  "Company":{   
    "address":"milton street",   
    "postal code":"cincinnati",   
    "name":"abc"  
  }  
}  

Current Output:

{  
"Company":{   
"address":"milton street",    
"postal code":"cincinnati",    
"name":"abc"  
},   
"id":"14",   
"email":"[email protected]"  
} 

Here is my code:

 JSONObject First = new JSONObject();
    First.put("id", "14");
    First.put("email", "[email protected]");

    JSONObject companydetails = new JSONObject();
    companydetails.put("name", "abc");
    companydetails.put("address", "milton street");
    companydetails.put("postal code", "cincinnati");

    First.put("Company",companydetails);

    System.out.println(First.toString());

Upvotes: 0

Views: 558

Answers (4)

Dev_java
Dev_java

Reputation: 11

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.Builder;
import lombok.Getter;
import org.junit.Test;

public class TestJUnit {
    @Test
    public void exec() {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();

        String toPrint = gson.toJson(ToJsonFormat.builder()
                .id(14)
                .email("[email protected]")
                .company(Company.builder()
                        .address("milton street")
                        .postCode("cincinnati")
                        .name("abc")
                        .build())
                .build());

        System.out.println(toPrint);
    }

    @Getter
    @Builder
    private static class ToJsonFormat {
        private int id;
        private String email;
        private Company company;
    }

    @Getter
    @Builder
    private static class Company {
        private String address;
        @JsonProperty("post code")
        private String postCode;
        private String name;
    }
}

result:

{"id":14,"email":"[email protected]","company":{"address":"milton street","post_code":"cincinnati","name":"abc"}}

Upvotes: 0

johannesr
johannesr

Reputation: 21

See the answer here: JSON order mixed up

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

From the JSON specification at http://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.

Upvotes: 1

ArunKumar M N
ArunKumar M N

Reputation: 1266

use google/gson library

JsonObject o=new JsonObject();
o.addProperty("id", "14");
o.addProperty("email", "[email protected]");

JsonObject companydetails1 = new JsonObject();
companydetails1.addProperty("name", "abc");
companydetails1.addProperty("address", "milton street");
companydetails1.addProperty("postal code", "cincinnati");

o.add("Company",companydetails1);
System.out.println(o.toString());

with maven repo

    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>

or with the external jar download

Upvotes: 1

Bishal Jaiswal
Bishal Jaiswal

Reputation: 1873

JsonObject doesn't maintain the order, and it doesn't matter as you will be accessing the json by keys.

Upvotes: 0

Related Questions