2

For example I have the following json response :

{
    StaTuS:succees,
    LanGuaGes: {
          Key1: English,
          key2: Spanish,
          kEy3: Indian
    }
} 

The response can have many nested elements. I want to know how we can code in such a way that all the keys can be converted to uppercase in my response so that it matches the naming convention I used in my POJO class.

Like this :

{
   STATUS:succees,
   LANGUAGES: {
          KEY1: English,
          KEY2: Spanish,
          KEY3: Indian
   }
} 
Palak
  • 25
  • 1
  • 1
  • 8
  • You can write a regex to find all the words which end with a : and convert these to uppercase, but to do this properly you should use a JSON parser though this would change the format. – Peter Lawrey Jan 08 '16 at 13:10
  • @Peter Lawrey is there any other way? like using JsonNode or ObjectNode? or any such nodes available in JSON/JACKSON lib. – Palak Jan 08 '16 at 13:12
  • If you are using a parser already I would would use that. – Peter Lawrey Jan 08 '16 at 16:05

3 Answers3

4

You can use a custom PropertyNamingStrategy:

public class UpperCaseStrategy extends PropertyNamingStrategyBase {

    @Override
    public String translate(String propertyName) {

        return propertyName.toUpperCase();
    }

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(new CustomNamingStrategy());

See here for reference.

As a note a lower case strategy is implemented in com.fasterxml.jackson.databind.PropertyNamingStrategy as follows:

/**
 * Simple strategy where external name simply only uses lower-case characters,
 * and no separators.
 * Conversion from internal name like "someOtherValue" would be into external name
 * if "someothervalue".
 * 
 * @since 2.4
 */
public static class LowerCaseStrategy extends PropertyNamingStrategyBase
{
    @Override
    public String translate(String input) {
        return input.toLowerCase();
    }
}
Ghokun
  • 3,345
  • 3
  • 26
  • 30
  • Actually the problem is I can receive the json response from client in any format (Uppercase, lower case , capitalize each word). The answer you posted will convert the naming strategy in my POJO classes to any one format. But I want to convert the json response to match my POJO class naming strategy. I hope you got my point here. Thanks for you help. – Palak Jan 08 '16 at 14:13
1

I know it's an old question, but in case it helps.

public JsonArray jsonKeysUpper(JsonArray arr) {
    JsonArray aux = new JsonArray();
    for(int i = 0; i < arr.size(); ++i)
        aux.add(jsonKeysUpper(arr.get(i).getAsJsonObject()));
    return aux;
}

public JsonObject jsonKeysUpper(JsonObject obj) {
    JsonObject aux = new JsonObject();
    if(obj.isJsonPrimitive()) return obj;
    if(obj.isJsonNull()) return null;
    // Iterate all properties
    for(String o : obj.keySet()) {
        if(obj.get(o).isJsonPrimitive())
            aux.addProperty(o.toUpperCase(), obj.get(o).getAsString());
        else if(obj.get(o).isJsonArray())
            aux.add(o.toUpperCase(), jsonKeysUpper(obj.get(o).getAsJsonArray()));
        else
            aux.add(o.toUpperCase(), jsonKeysUpper(obj.get(o).getAsJsonObject()));
    }
    return aux;
}
0

Use below code to convert your json to Upper case String keys json

ObjectMapper obj = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); 
obj.convertValue(jsonString, Map.class);
Harsh Garg
  • 31
  • 1
  • 4