0

I have a JSON String like this in which I have key and value as shown below -

{
  "u":{
     "string":"1235"
  },
  "p":"2047935",
  "client_id":{
     "string":"5"
  },
  "origin":null,
  "item_condition":null,
  "country_id":{
     "int":3
  },
  "timestamp":{
     "long":1417823759555
  },
  "impression_id":{
     "string":"2345HH*"
  },
  "is_consumerid":true,
  "is_pid":false
}

As an example, one key is "u" and its value is -

{
    "string":"1235"
}

Similarly another key is "country_id" and its value is -

{
    "int":3
}

Similarly for others as well. Now what I need to do is, I need to represent the above key value as shown below. If any value is string data type, then represent it in double quotes, otherwise don't represent it in double quotes

"u": "1235"
"p": "2047935"
"client_id": "5"
"origin":null
"item_condition":null   
"country_id": 3 // I don't have double quotes here around 3 since country_id was int that's why
"timestamp": 1417823759555
"impression_id": "2345HH*"
"is_consumerid": true
"is_pid": false

And then I need to make another json string which should look like this -

{
    "u": "1235",
    "p": "2047935",
    "client_id": "5",
    "origin":null,
    "item_condition":null,      
    "country_id": 3,
    "timestamp": 1417823759555,
    "impression_id": "2345HH*",
    "is_consumerid": true,
    "is_pid": false
}

What is the best and efficient way to do this?

Update:-

This is what I have got -

    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();

    for (Map.Entry<String, JsonElement> object : jsonObject.entrySet()) {
        if (object.getValue() instanceof JsonObject) {
            String data = object.getValue().toString();
            Map<String, Object> jsonIn = gson.fromJson(data, type);
            Map<String, Object> jsonOut = new HashMap<String, Object>();

            Set<String> keys = jsonIn.keySet();
            for (String key : keys) {
                Object value = jsonIn.get(key);
                if (value instanceof Map) {
                    Map<?, ?> mapValue = (Map<?, ?>) value;
                    for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
                        jsonOut.put(key, entry.getValue());
                    }
                } else {
                    jsonOut.put(key, value);
                }
            }

            // System.out.println(jsonOut);
            Type typeOfMap = new TypeToken<Map<String, String>>() {
            }.getType();
            String json = gson.toJson(jsonOut, typeOfMap);
            System.out.println(json);

        }
    }

Only thing which is not working is - when I try to serialize jsonOut map to a JSON, then all the values of a particular key gets converted to a string. But what I need is, only those values should get converted to a string (double quotes) which they are in general like country_id value should not be in double quotes since it is an integer as per my original JSON.

2
  • Parse into Maps, write code to create the new Map, serialize the new Map. 5-10 minutes of work. Commented Dec 6, 2014 at 1:27
  • @HotLicks Thanks for suggestion. Can you provide a simple example which can help me to understand how to do this? Commented Dec 6, 2014 at 1:28

1 Answer 1

1

Pseudocode:

Map<Object> jsonIn = JsonParser.parseJsonToMap(theJson);
Map<Object> jsonOut = new HashMap<Object>();
String[] keys = jsonIn.getKeys();
for (String key in keys) {
    Object value = jsonIn.get(key);
    if (value instanceof Map) {
        Map mapValue = (Map) value;
        Object[] valueValues = mapValue.getValues();
        assert valueValues.length == 1;
        jsonOut.add(key, valueValues[0]);
    }
    else {
        jsonOut.add(key, value);
    }
}

This code cheats a little by relying on the fact that one shouldn't need to manipulate the data types, since they're already correct. One could add more logic to inspect the key values of the inner Maps to determine the data types, but I see no point, unless some sort of data type translation is needed.

Some JSON kits have their own classes, such as JSONObject, that they use instead of Maps, but they're basically just subclasses of Map and the logic is the same (though the instanceof would be replaced by querying the type of the value, and the (Map) cast would be replaced by a specific getObject method call or some such.

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

7 Comments

I got this working. Now I am stuck at how do I convert jsonOut map to a JSON String using GSON?
@user2809564 - I don't try to keep the two dozen different JSON kits for Java straight. But if you have jsonOut in a JSONObject it looks like you'd just do toString() on it. If you have it as a Map it look like you'd do GSON.toJson(jsonOut). (Perhaps you'd do GSON.toJSON on a JSONObject as well, I can't tell.)
Yup I did some googling figure that one out. One thing which is not working with this approach is - in jsonOut map, how do I make sure that some key has int value and some key has string value? Meaning, when I try to serialize jsonOut, it should have some key value as int and some key value as string in double quotes? How would we do that?
@user2809564 - I was relying on the types being preserved through the translation. The JSON toolkit has the responsibility to add the quotes around String values, and should be doing it appropriately. (And note that JSON only permits strings for key values.)
I have updated the question with the code I have. Is there any way I can preserve the data types of particular key?
|

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.