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
lrente
1,1501 gold badge11 silver badges30 bronze badges
3 Answers 3
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
chaitanya89
8474 gold badges21 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
mzherdev
4621 gold badge4 silver badges11 bronze badges
Comments
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
Aslami.dev
1,0801 gold badge9 silver badges19 bronze badges
Comments
lang-java
["Name : Eb1", "Prop2 : Val1", "PropN : ValN"]to{"Name": "Eb1","Prop2": "Val1","PropN": "ValN"}. All the other stuff is just clutter.