Pawan
Pawan

Reputation: 32321

Without looping JSON Array how to get appropiate value from JSON Array

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

Upvotes: 0

Views: 1913

Answers (2)

George Andrews
George Andrews

Reputation: 324

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.

Upvotes: 1

Stefan Gro&#223;mann
Stefan Gro&#223;mann

Reputation: 1026

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());

Upvotes: 0

Related Questions