2

I've a JSONArray and I need to fetch if particular key value is available in this JSONArray. Lets say, I have a specific key value pair like Current Employment Status:False and I need to verify if this key value pair is available in my JSONArray or not. My JSONArray is below:

[
   {
      "last_seen":1557908766238,
      "distinct_id":"11cb639f-cbf7-499c- a1cf-e3ff4466a093",
      "time":1557908766238,
      "properties":{
         "$browser_version":74,
         "$name":"Jessica Claire",
         "$timezone":"Asia/Kolkata",
         "$os":"Windows",
         "$email":"[email protected]",
         "$initial_referring_domain":"$direct",
         "userid":"11cb639f-cbf7-499c-a1cf-e3ff4466a093",
         "$first_name":"Jessica",
         "Current Employment Status":"FALSE",
         "enter builder":1,
         "fedex experience":"FALSE",
         "ResumeTips Permission":"TRUE",
         "id":"11cb639f-cbf7-499c-a1cf-e3ff4466a093",
         "LCNews Permission":"TRUE",
         "Document Creation Mode":"scratch",
         "Email Valid":"False",
         "$last_name":"Claire",
         "Payment Status":"Not Started",
         "Work Experience Modal":"TRUE",
         "$city":"Noida",
         "Subscription Status":"FALSE",
         "Job Alerts Permission":"TRUE",
         "sign up":1,
         "$browser":"Chrome",
         "userId":"11cb639f-cbf7-499c-a1cf-e3ff4466a093",
         "$country_code":"IN",
         "$region":"Uttar Pradesh",
         "One Time Purchase":"FALSE",
         "$last_seen":"2019-05-15T08:26:06.000Z",
         "Last enter builder":"2019-05-15T08:24:01.000Z",
         "Last sign up":"2019-05-15T08:26:03.000Z",
         "$initial_referrer":"$direct",
         "EduOps Permission":"TRUE"
      },
      "labels":[

      ]
   }
]

It should print true if present and false if not.

3 Answers 3

1

I had stored this json in a file named findKey.json

        String path = "others\\findKey.json";
        File f = new File(path);
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader(f));

        JSONArray arr = (JSONArray) obj;
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i) instanceof JSONObject) {
                JSONObject jsonobject = (JSONObject) arr.get(i);
                JSONObject props = (JSONObject) jsonobject.get("properties");
                String key = (String) props.get("Current Employment Status");
                //print true if key is found and equals to false
                Boolean output= (key != null && key.equals("FALSE"))? true:false;
                System.out.println(output);

            }
        }

don't forget to use json-simple

Sign up to request clarification or add additional context in comments.

Comments

1

You can use JsonArray and JsonObject to do that. Use org.json maven dependency.

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

    public static void main(String[] args) throws IOException, JSONException {

        String sb = "[{\"last_seen\":1557908766238,\"distinct_id\":\"11cb639f-cbf7-499c- a1cf-e3ff4466a093\",\"time\":1557908766238,\"properties\": {\"$browser_version\":74,\"$name\":\"Jessica Claire\",\"$timezone\":\"Asia/Kolkata\",\"$os\":\"Windows\",\"$email\":\"[email protected]\",\"$initial_referring_domain\":\"$direct\",\"userid\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$first_name\":\"Jessica\",\"Current Employment Status\":\"FALSE\",\"enter builder\":1,\"fedex experience\":\"FALSE\",\"ResumeTips Permission\":\"TRUE\",\"id\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"LCNews Permission\":\"TRUE\",\"Document Creation Mode\":\"scratch\",\"Email Valid\":\"False\",\"$last_name\":\"Claire\",\"Payment Status\":\"Not Started\",\"Work Experience Modal\":\"TRUE\",\"$city\":\"Noida\",\"Subscription Status\":\"FALSE\",\"Job Alerts Permission\":\"TRUE\",\"sign up\":1,\"$browser\":\"Chrome\",\"userId\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$country_code\":\"IN\",\"$region\":\"Uttar Pradesh\",\"One Time Purchase\":\"FALSE\",\"$last_seen\":\"2019-05-15T08:26:06.000Z\",\"Last enter builder\":\"2019-05-15T08:24:01.000Z\",\"Last sign up\":\"2019-05-15T08:26:03.000Z\",\"$initial_referrer\":\"$direct\",\"EduOps Permission\":\"TRUE\"},\"labels\":[]}]";
        JSONArray jsonArray = new JSONArray(sb);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Object value = null;
            try {
                JSONObject properties = (JSONObject) jsonObject.get("properties");
            value = properties.get("Current Employment Status");

            } catch (JSONException e) {
                //print flase if not found
                System.out.println("false");
            }
            //print the value if found
            System.out.println("Value =" + value);

        }

    }
}

1 Comment

This is not working, if you observe there is further jsonObject inside this jsonarray with the name of Properties and "Current Employment Status" value is inside that json. I need to verify "Current Employment Status" along with the value("false" in this json). Your help is appreciated
0

I Have used a jar to parse String into JSon

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class test {
public static void main(String[] args) {
    String temp = "[{\"last_seen\":1557908766238,\"distinct_id\":\"11cb639f-cbf7-499c- "
            + "a1cf-e3ff4466a093\",\"time\":1557908766238,\"properties\": {\"$browser_version\":74,"
            + " \" $name\":\"Jessica Claire\",\"$timezone\":\"AsiaKolkata\","
            + "\"$os\":\"Windows\",\"$email\":\"[email protected]\",\"$initial_referring_domain\":\"$direct\",\"userid\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$first_name\":\"Jessica\",\"Current Employment Status\":\"FALSE\",\"enter builder\":1,\"fedex experience\":\"FALSE\",\"ResumeTips Permission\":\"TRUE\",\"id\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"LCNews Permission\":\"TRUE\",\"Document Creation Mode\":\"scratch\",\"Email Valid\":\"False\",\"$last_name\":\"Claire\",\"Payment Status\":\"Not Started\",\"Work Experience Modal\":\"TRUE\",\"$city\":\"Noida\",\"Subscription Status\":\"FALSE\",\"Job Alerts Permission\":\"TRUE\",\"sign up\":1,\"$browser\":\"Chrome\",\"userId\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$country_code\":\"IN\",\"$region\":\"Uttar Pradesh\",\"One Time Purchase\":\"FALSE\",\"$last_seen\":\"2019-05-15T08:26:06.000Z\",\"Last enter builder\":\"2019-05-15T08:24:01.000Z\",\"Last sign up\":\"2019-05-15T08:26:03.000Z\",\"$initial_referrer\":\"$direct\",\"EduOps Permission\":\"TRUE\"},\"labels\":[]}]";

String findkeyvalue = "Current Employment Status:False";


boolean flag =  FindValue(temp , findkeyvalue);

System.out.println(flag);
}

private static boolean FindValue(String temp, String findkeyvalue) {
    try {
        JSONParser jsonParser = new JSONParser();

        JSONArray array = (JSONArray) jsonParser.parse(temp);

        for (int i = 0; i < array.size(); i++) {
            System.out.println(array.get(i).toString());
             return  travertillLast(array.get(i).toString() , findkeyvalue);
        }

    } catch (Exception e) {
        return false;
    }
    return false;
}

private static boolean   travertillLast(String string, String findkeyvalue) throws ParseException {
    JSONParser jsonParser = new JSONParser();

    JSONObject jsonObj = (JSONObject) jsonParser.parse(string);

    System.out.println(jsonObj);
       for (Object key : jsonObj.keySet()) {
            //based on you key types
            String keyStr = (String)key;
            Object keyvalue = jsonObj.get(keyStr);

            //Print key and value
            String keyandValuue = keyStr + ":" + keyvalue;
            System.out.println(keyandValuue);
            if(keyandValuue.trim().equalsIgnoreCase(findkeyvalue)){
                return true;
            }

            //for nested objects iteration if required
            if (keyvalue instanceof JSONObject)
                return travertillLast(keyvalue+"",findkeyvalue);
        }
    return false;
    // TODO Auto-generated method stub

}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.