1

How can I extract JSON Array and JSON Object from JSON.

Below is the input:

{
    "messageName": "ReportCard",
    "orgId": "Org1",
    "comment": true,
    "Fields": [{
       "objectId": "1234-56789-asdv",
       "fieldId": "1245-7852-dhjd"
    },
    {
       "objectId": "1234-56hgjgh789-hjjhj",
       "fieldId": "12sdf45-78sfg52-dfjhjd"
    }]
}

I want JSON Array and JSON Object separately and output should be like:

JSONArray

"Fields":[{ "objectId": "1234-56789-asdv",
           "fieldId": "1245-7852-dhjd"},{
           "objectId": "1234-56hgjgh789-hjjhj",
           "fieldId": "12sdf45-78sfg52-dfjhjd"}]

and JSON Object should be like:

{
  "messageName": "ReportCard",
        "orgId": "Org1",
        "comment": true
}
Abhinav
  • 530
  • 8
  • 21
RAHUL GUPTA
  • 49
  • 11
  • You should share code you tried to apply against given problem. Can you also put more details about what kind of json library you use. – rxn1d Nov 15 '18 at 06:08
  • Possible duplicate of [How to Get JSON Array Within JSON Object?](https://stackoverflow.com/questions/32624166/how-to-get-json-array-within-json-object) – Abhinav Nov 15 '18 at 06:09
  • i'm using "json-simple" Library. and input is a @requestParam parameter of post api of Springboot project ,i want to save jsonObject in a different Collection and JsonArray in different Collection of mongodb. JSONObject is data member of a model class and JSON Array is a Diffrent model class in the Project – RAHUL GUPTA Nov 15 '18 at 06:14
  • @Thekamble can you suggest what should be right format of it?? Thanks – RAHUL GUPTA Nov 15 '18 at 06:19
  • @Abhinav I think that person wants to combine it and i want to split it. – RAHUL GUPTA Nov 15 '18 at 06:21

2 Answers2

2

its pretty simple if you know java JSON API

String jsonString="{
    "messageName": "ReportCard",
    "orgId": "Org1",
    "comment": true,
    "Fields": [{
       "objectId": "1234-56789-asdv",
       "fieldId": "1245-7852-dhjd"
    },
    {
       "objectId": "1234-56hgjgh789-hjjhj",
       "fieldId": "12sdf45-78sfg52-dfjhjd"
    }]
}"
JSONObject jObject= new JSONObject(jsonString);
JSONObject jo = new JSONObject(); //creating new Jobject
// putting data to JSONObject 
jo.put("messageName", jObject.getString("messageName").toString()); 
jo.put("orgId", jObject.getString("orgId").toString()); 
jo.put("comment", jObject.getString("comment").toString()); 

JSONArray Fields= jObject.getJSONArray("Fields");//extract field array
JSONArray ja = new JSONArray(); //creating new json array.
int Arraylength = Fields.length();
for(int i=0;i<Arraylength;i++)
{
    Map m = new LinkedHashMap(2); 
    JSONObject ArrayjObj = Fields.getJSONObject(i);
    m.put("objectId", ArrayjObj.getString("objectId").toString()); 
    m.put("fieldId", ArrayjObj.getString("fieldId").toString()); 
    // adding map to list 
    ja.add(m); 
 }
JSONObject fieldsObj = new JSONObject(); 
fieldsObj.put("Fields", ja); // Fields Array Created

for JSON api refer this

Negi Rox
  • 3,828
  • 1
  • 11
  • 18
1

you can fetch particular values as per keys into a json object and rest into a separate json array

String strJSON =" {\"id\":\"12\",\"messageName\":\"ReportCard\" , \"Fields\":[{\"objectId\": \"1234-56789-asdv\", \"fieldId\": \"1245-7852-dhjd\"},{\"objectId\": \"1234-56hgjgh789-hjjhj\", \"fieldId\": \"12sdf45-78sfg52-dfjhjd\"}]   }";

 JSONArray ja = new JSONArray();
JSONObject jo1= new JSONObject();
JSONObject jo= new JSONObject(strJSON);
  ja=  jo.getJSONArray( "Fields");
jo1.put("messageName",jo.get(messageName));
jo1.put("orgId",jo.get(orgId));
purvaB
  • 49
  • 4