0

I have a JSON as follows .

[{
        "empid": "1",
        "name": "Mark"

    },
    {
        "empid": "2",
        "name": "Steve"

    },
    {
        "empid": "1",
        "name": "Luke "

    }
]

Based on the empid as input , i need to get appropiate name

I have tried this its working .

But instead of looping is there any simple way ??

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONPArseEx {
    public static void main(String[] args) throws JSONException {
        String empIdInput = "2";

        String jsonStr = "[{\r\n" + 
                "       \"empid\": \"1\",\r\n" + 
                "       \"name\": \"Mark\"\r\n" + 
                "       \r\n" + 
                "   },\r\n" + 
                "   {\r\n" + 
                "       \"empid\": \"2\",\r\n" + 
                "       \"name\": \"Steve\"\r\n" + 
                "       \r\n" + 
                "   },\r\n" + 
                "   {\r\n" + 
                "       \"empid\": \"1\",\r\n" + 
                "       \"name\": \"Luke \"\r\n" + 
                "       \r\n" + 
                "   }\r\n" + 
                "]";

    JSONArray jsonarray = new JSONArray(jsonStr);
    for(int i=0;i<jsonarray.length();i++)
    {
        JSONObject json_obj = jsonarray.getJSONObject(i);
        if(json_obj.get("empid").equals(empIdInput))
        {
            System.out.println(json_obj.get("name"));
        }
    }
    }
}

Output is

Steve

2
  • have you tried with streams? Commented Dec 18, 2017 at 14:54
  • 2
    Have you considered JSONPath? $.[?(@.empid==1)].name is something that will do the same for you Commented Dec 18, 2017 at 15:04

2 Answers 2

1

You will have to iterate over the array to check, but if you don't want to use a loop - you could use Java 8 Streams:

import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JSONStreamExample {

     private static final String empIdInput = "2";

     private static final String jsonStr = "[{\r\n" + 
         "       \"empid\": \"1\",\r\n" + 
         "       \"name\": \"Mark\"\r\n" + 
         "       \r\n" + 
         "   },\r\n" + 
         "   {\r\n" + 
         "       \"empid\": \"2\",\r\n" + 
         "       \"name\": \"Steve\"\r\n" + 
         "       \r\n" + 
         "   },\r\n" + 
         "   {\r\n" + 
         "       \"empid\": \"1\",\r\n" + 
         "       \"name\": \"Luke \"\r\n" + 
         "       \r\n" + 
         "   }\r\n" + 
         "]";

/**
 * @param args
 */
public static void main(final String[] args) {

        @SuppressWarnings("unchecked")
        List<JSONObject> jsonArray = (JSONArray) JSONValue.parse(jsonStr);
        JSONObject result = jsonArray.stream()
                                .filter(obj -> obj.get("empid").equals(empIdInput))
                                .findFirst()
                                .orElse(null);
        if (result != null) {
            System.out.println(result.get("name"));
        }
    }

}

Note that this uses json-simple which you can find on the maven repository here.

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

Comments

0

A more convenient processing could to use the stream API (since Java 8). For examle:

final Optional<JsonObject> result = empArray.stream()
        .map(empVal -> (JsonObject) empVal)
        .filter(emp -> empIdInput.equals(emp.getString("empid")))
        .findAny();

result.ifPresent(emp -> System.out.println(emp.get("name")));

To work around the unchecked cast you could add some filtering:

final Optional<JsonObject> result = empArray.stream()
        .filter(empVal -> empVal instanceof JsonObject)
        .map(empVal -> (JsonObject) empVal)
        .filter(emp -> empIdInput.equals(emp.getString("empid")))
        .findAny()
        .ifPresent(emp -> System.out.println(emp.get("name")));

As "empid" seems not to be unique you might want to get a list of results:

final List<JsonObject> result = empArray.stream()
        .filter(empVal -> empVal instanceof JsonObject)
        .map(empVal -> (JsonObject) empVal)
        .filter(emp -> empIdInput.equals(emp.getString("empid")))
        .collect(Collectors.toList());

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.