How to ignore case when searching a JSON Object

Chaos picture Chaos · Aug 5, 2013 · Viewed 24.6k times · Source

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.

Answer

Chris Gerken picture Chris Gerken · Aug 6, 2013

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;

}