0

I'm trying to create following Json from a string which is coming from an Excel

{
                "RequestId": "1234",
                "OrderID": "NCBTEST02082018-01",
                "Assets": [{
                                "Field1":"1",
                                "Field2": "NCBTEST-01",
                                "Field3": "1",
                                "Field4": "1",
                                "Field5": "1",
                                "Field6": "1",
                                "Field7": "1"
                }] }

I am able to create assets araay but I don't know how to add first 2 fields which are "RequestId": "1234", and "OrderID": "NCBTEST02082018-01", before the assets array.

Siddhesh
  • 1,095
  • 1
  • 11
  • 23

4 Answers4

0

I'm not sure what you're exactly doing, but i should be something like this:

JsonObject value = Json.createObjectBuilder()
  .add("RequestId", "1234")
  .add("OrderId", "NCB1234")
  .add("Assets", Json.createArrayBuilder()
    .add(Json.createObjectBuilder()
      .add("Field1", "1") // add more in loop
  ).build();
0

If you're using a org.codehaus.jettison.json.JSONObject, just do:

new JSONObject(jsonString);

where jsonString is a String containing your entire json object.

Jan Gassen
  • 3,406
  • 2
  • 26
  • 44
0

You can use new JSONObject(string) to get the required json object Now coming to order , if you are talking about JSONObject then order is not important as the values are stored under keys which can be accessed randomly unaffected by order You can create a new JSONObject then add requestid and orderid before adding the assets array

JSONObject y=new JSONObject('');
y.put('requestid','whateer');
y.put('orderid','whatever');
y.put('assets',myjsonarray)
Abhishek Mathur
  • 478
  • 5
  • 12
0

Below are just key-value pairs:

"RequestId": "1234"

"OrderID": "NCBTEST02082018-01"

So, you can insert them in the JSONObject.

Something like this:

JSONObject jb = new JSONObject();
        JSONArray ja = new JSONArray();
        jb.put("Field1", "1");
        jb.put("Field2", "NCBTEST-01");
        jb.put("Field3", "1");
        jb.put("Field4", "1");
        jb.put("Field5", "1");
        jb.put("Field6", "1");
        jb.put("Field7", "1");
        ja.put(jb);


        JSONObject main = new JSONObject();
        main.put("RequestId", "1234");
        main.put("OrderID", "NCBTEST02082018-01");
        main.put("Assets", ja);

        System.out.println(main);

Output:

{
    "RequestId": "1234",
    "Assets": [{
        "Field4": "1",
        "Field5": "1",
        "Field2": "NCBTEST-01",
        "Field3": "1",
        "Field6": "1",
        "Field7": "1",
        "Field1": "1"
    }],
    "OrderID": "NCBTEST02082018-01"
}
Rahul
  • 637
  • 5
  • 16
  • Worked! but why those fields are not coming in order in which we are putting them? – Siddhesh Feb 14 '18 at 15:00
  • As per JSON specification, "An object is an unordered set of name/value pairs". Check: http://www.json.org/ , https://stackoverflow.com/questions/3948206/json-order-mixed-up – Rahul Feb 14 '18 at 15:15