9

My sample JSON input is as follows:

"JobName":"Test Job 1",
"events":[
    {   "features":[],
        "InputHiveTable":"uilog_uiclientlogdata",
        "eventColumn":"command",
        "name":"edu.apollogrp.classroom.discussion.client.events.CreateDiscussionEvent"
    },

Consider the field "InputHiveTable", it could be in all uppercase INPUTHIVETABLE, all lowercase inputhivetable, or a mixture of both as it is now.

Currently, I'm reading the field as follows (in Java):

JSONObject jsonObject = (JSONObject) obj;
JSONArray events = (JSONArray) jsonObject.get("events");
String InputHiveTable = (String)event.get("InputHiveTable");

So my question is how do I search for the field "InputHiveTable" while ignoring the case. I'm using JSON Simple libraries.

Chaos
  • 11,213
  • 14
  • 42
  • 69

4 Answers4

21

If you have to perform this case-insensitive lookup many times, I'd just write a method to do that lookup:

public Object getIgnoreCase(JSONObject jobj, String key) {

    Iterator<String> iter = jobj.keySet().iterator();
    while (iter.hasNext()) {
        String key1 = iter.next();
        if (key1.equalsIgnoreCase(key)) {
            return jobj.get(key1);
        }
    }

    return null;

}
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
3

You could read the JSONObject into a java string, and call String.toLowerCase on it and store it back into a JSONObject. This will turn the entire case of the string to lower case, so you will have to account for that elsewhere in your logic. After that, you then would just have to do a get call on "inputhivetable".

By no means is it a pretty solution, but it is a potential work around if there is absolutely no other way for you to handle what you're returning as your JSON input.

Zoltorn
  • 171
  • 1
  • 2
  • 10
  • 3
    That would have the distinct disadvantage of lower-casing the values, as well as the keys. There would seem to be no easy way to do a case-insensitive match on the keys alone, without getting more fancy such as iterating over the keys or changing the parser. – BobDoolittle Jan 28 '16 at 21:36
  • Does the job when the formatting of the values are not important. In my case, all my values are numbers. – Tolio Oct 10 '16 at 19:59
  • That's the solution I used because values also in my situation are case insensitive. – DDS May 14 '21 at 06:18
2

Given that case-insensitivity can be achieved with TreeMap (i.e. via String.CASE_INSENSITIVE_ORDER comparator), you can probably do the following:

  1. Implement your own MyJSONObject extending TreeMap where its methods will be just calling static methods of JSONObject with the same signatures and all required interfaces as in JSONObject. In default constructor write super(String.CASE_INSENSITIVE_ORDER)

  2. Implement ContainerFactory interface where createObjectContainer will return new instance of MyJSONObject (and createArrayContainer will just return new JSONArray).

  3. To run it with new container MyContainerFactory:

     StringReader in = new StringReader(yourJSONString);                    
     JSONParser parser = new JSONParser();      
     parser.parse(in, yourContainerFactory)
    
Alex P
  • 1,721
  • 1
  • 17
  • 18
0

Example Json : "data": { "Information": { "Id": "64". "relation": "Current",} }

In the java model :

@JsonbProperty("Information")
private DataInformationModel information;
@JsonbProperty("Id")
private DataModel id;

@JsonbProperty worked for me