-1

Below is my json format code:

{
    "topic": "Employee",
    "message": {
        "Id": "IND01",
        "data": {
            "salary": 50000
        }
    }
}

The key names such as topic, message, Id and data to be read as case-insensitive. How can I get the keys from this json response body ?

Shashank Goyal
  • 153
  • 1
  • 5
Raksh
  • 19
  • 2
  • 5

3 Answers3

0

First of all your JSON is not vaild.I believe that is your question and it will solve your other problems. Try

{
    "topic": "Employee",
    "message": {
        "Id ": "IND01",
        "data ": {

            "salary ": 50000
        }
    }
}

This is a valid JSON in your case.Henceforth learn the correct format of JSON and use any json validator to validate your JSON.

Akshay
  • 1,161
  • 1
  • 12
  • 33
0

If you can convert your json to a Map, you can remap the whole map (recursively) transforming every key to its lowercase value.

Something like :

public static Map<String,Object> convertToLowerCaseKeys(Map<String,Object> map){
    return map.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toLowerCase(), e -> {
        if(e.getValue() != null && e.getValue() instanceof Map){
            return convertToLowerCaseKeys((Map<String,Object>)e.getValue());
        }
        return e.getValue();
    }));
}

Note that the cast to Map might be a little too hasty but I've gone with it for simplicity.

EDIT :

To search for a (top-level) key while being case insensitive :

public static Object getCaseInsensitive(Map<String,Object> map, String key){
    if(key == null){
        return map.get(null);
    }
    Optional<String> optional = map.keySet().stream().filter(e -> e != null && e.toLowerCase().equals(key.toLowerCase())).findAny();
    if(optional.isPresent()){
        return map.get(optional.get());
    }
    return null;
}
Jeremy Grand
  • 2,300
  • 12
  • 18
  • How about for reading a particular key name as case-insensitive? – Raksh Feb 20 '17 at 12:25
  • Check for equality between the actual key converted to lowercase and the key you want to test also converted to lowercase – Jeremy Grand Feb 20 '17 at 12:32
  • can you please elaborate the answer @Jeremy Grand – Raksh Feb 20 '17 at 12:38
  • I am calling an Rest API with post method in which I am passing this json format as request body. can you please suggest me, where exactly should I include this getCaseInsensitive method @Jeremy Grand – Raksh Feb 20 '17 at 12:53
  • The caseInsensitive read has to be done behind the API. Are you the author of the API you are posting to ? Or have you just read in their documentation that the json keys were case-insensitive ? – Jeremy Grand Feb 20 '17 at 13:04
  • Yes I am the author of that API. – Raksh Feb 20 '17 at 13:10
  • Well, the Service behind that API that needs to read values from the Map has to call the getCaseInsensitive method. Without knowing your whole architecture, it is hard to give an answer. I'd advise you to convert the whole map to lower case keys using the method in my first answer and simply use it as it is in the rest of your application. – Jeremy Grand Feb 20 '17 at 13:14
0

You can read names of members by getMembersNames()

see below some code

auto members = root.getMemberNames();

for(Json::Value::Members::iterator iter = members.begin();iter != members.end();iter++) {
  if(_stricmp(iter->c_str(), “id”)==0)
    id = root[iter->c_str()].asInt();
}