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
}
5
  • 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. Commented Nov 15, 2018 at 6:08
  • Possible duplicate of How to Get JSON Array Within JSON Object? Commented Nov 15, 2018 at 6: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 Commented Nov 15, 2018 at 6:14
  • @Thekamble can you suggest what should be right format of it?? Thanks Commented Nov 15, 2018 at 6:19
  • @Abhinav I think that person wants to combine it and i want to split it. Commented Nov 15, 2018 at 6:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.