2

I have JSONArray in String format as follows :

{
    "productsList": [{
            "map": {
                "productSubcategory": "Levensverzekering",
                "nameFirstInsured": "Akkerman"
            }
        },
        {
            "map": {
                "productSubcategory": "Lineair dalend",
                "nameFirstInsured": "Akkerman"
            }
        }
    ]
}

I want to convert this String as follows :

{
    "productsList": [{

            "productSubcategory": "Levensverzekering",
            "nameFirstInsured": "Akkerman"

        },
        {

            "productSubcategory": "Lineair dalend",
            "nameFirstInsured": "Akkerman"

        }
    ]
}

I have converted JSONArray to String so need operation as on the String on provided String in JSON format. How I can change the String as required? What should I put in jsonString.replaceAll("","") function?

3
  • 2
    No, you should not be doing this on strings to begin with. Work on the actual object, get that into the form you want it to be - and then encode it as JSON. Commented Sep 20, 2017 at 11:57
  • JsonArray actually doesn't contain that "map" word but when I apply it the Gson, it automatically takes it as various different objects. That's why I need operation on String itself please.. Commented Sep 20, 2017 at 12:02
  • No clue what you are trying to say here. A key does not magically appear when an object is converted to JSON - that object would have had to have that key to begin with. Commented Sep 20, 2017 at 12:05

1 Answer 1

1

There is no easy way to do this, you have to do something like this.

OUTPUT IS:

{  
   "productsList":[  
      {  
         "productSubcategory":"Levensverzekering",
         "nameFirstInsured":"Akkerman"
      },
      {  
         "productSubcategory":"Lineair dalend",
         "nameFirstInsured":"Akkerman"
      }
   ]
}



import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;


public class test {

    public static void main(String[] args) throws IOException, InterruptedException {
        JSONParser parser = new JSONParser();
        JSONObject newObj = new JSONObject();
        try {
            Object obj = parser.parse(new FileReader("test.json"));
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray arr = (JSONArray) jsonObject.get("productsList");
            JSONArray newArr = new JSONArray();
            for(int i = 0 ; i < arr.size();i++){
                JSONObject object = (JSONObject) arr.get(i);
                JSONObject a = (JSONObject) object.get("map");
                newArr.add(a);
            }
            newObj.put("productsList", newArr);
            System.out.println(newObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.