1

This seems quite simple but i'm struggling to transform this:

{
 "myList": [{
 "id": 1,
 "type": "EnergyBox",
 "properties": ["Name : Eb1", "Prop2 : Val1", "PropN : ValN"]
 }, {
 "id": 2,
 "type": "EnergyBox",
 "properties": ["Name : Eb2", "Prop2 : Val2", "Prop3 : Val3"]
 }]
}

into this:

{
 "myList": [{
 "id": 1,
 "type": "EnergyBox",
 "properties": {
 "Name": "Eb1",
 "Prop2": "Val1",
 "PropN": "ValN"
 }
 }, {
 "id": 2,
 "type": "EnergyBox",
 "properties": {
 "Name": "Eb2",
 "Prop2": "Val2",
 "Prop3": "Val3"
 }
 }]
}

I'm using Java 8, Jackson and Simplejson to parse the data, but somehow, and after many hours of trying, still not working.

asked Mar 2, 2018 at 10:57
3
  • 2
    Took me a while to see the difference. Why don't you remove the irrelevant stuff. Commented Mar 2, 2018 at 11:07
  • @Henry what irrelevant stuff? Can you elaborate? Commented Mar 2, 2018 at 11:23
  • Basically you want to transform ["Name : Eb1", "Prop2 : Val1", "PropN : ValN"] to {"Name": "Eb1","Prop2": "Val1","PropN": "ValN"}. All the other stuff is just clutter. Commented Mar 2, 2018 at 11:56

3 Answers 3

2

I tried to convert the Json as per your requirement, try the following code.

public void test() throws JsonParseException, JsonMappingException, IOException {
 String json = "{"+
 "\"myList\": [{"+
 "\"id\": 1,"+
 "\"type\": \"EnergyBox\","+
 "\"properties\": [\"Name : Eb1\", \"Prop2 : Val1\", \"PropN : ValN\"]"+
 "}, {"+
 "\"id\": 2,"+
 "\"type\": \"EnergyBox\","+
 "\"properties\": [\"Name : Eb2\", \"Prop2 : Val2\", \"Prop3 : Val3\"]"+
 "}]"+
 "}";
 ObjectMapper mapper = new ObjectMapper();
 JsonNode root = mapper.readTree(json);
 JsonNode myList = root.get("myList");
 myList.forEach(n -> {
 System.out.println(n);
 System.out.println(n.get("properties"));
 ObjectNode updatedProperties = mapper.createObjectNode();
 n.get("properties").forEach(p -> {
 String[] vals = p.textValue().split(":");
 updatedProperties.put(vals[0].trim(), vals[1].trim());
 });
 ((ObjectNode)n).put("properties", updatedProperties);
 });
 System.out.println(myList);
}
answered Mar 2, 2018 at 11:37
Sign up to request clarification or add additional context in comments.

Comments

2

Is it OK that key-value in properties like one string? For that purpose this should work:

 JSONParser parser = new JSONParser();
 JSONObject json = (JSONObject) parser.parse(jsonString);
 JSONArray array = (JSONArray) json.get("myList");
 for( int i = 0; i < array.size(); i++) {
 JSONObject object = (JSONObject) array.get(i);
 JSONArray jsonArray = (JSONArray) object.get("properties");
 JSONObject newJObj = new JSONObject();
 for(int j = 0; j < jsonArray.size(); j++) {
 String[] keyValue = jsonArray.get(j).toString().split(" : ");
 newJObj.put(keyValue[0], keyValue[1]);
 }
 newJObj.toJSONString();
 object.put("properties", newJObj);
 }
 json.toJSONString();
answered Mar 2, 2018 at 11:36

Comments

2
 JSONObject response= Array.getJSONObject(insert_your_Response);
 try {
 JSONArray Array = response.getJSONArray("myList");
 assert Array != null;
 for (int k = 0; k < Array.length(); k++) {
 try {
 JSONObject object1 = Array.getJSONObject(k);
 int id = object1.getInt("id");
 String type = object1.getString("type");
 JSONObject propertiesObject=object1.getJSONObject("properties");
 String Name = propertiesObject.getString("Name");
 String Prop2 = propertiesObject.getString("Prop2");
 String Prop3 = propertiesObject.getString("Prop3");
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
answered Mar 2, 2018 at 14:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.