Search code examples
javajsonjson-simple

How to ignore case when searching a JSON Object


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.


Solution

  • 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;
    
    }