Tej Kiran
Tej Kiran

Reputation: 2238

How to filter JSON object

I have a JSON object that is a list of Students that have Name, FName, City, Class, Contact. Now I want to filter only the objects (Students) which belong to the specific city. Can I filter the mail json object

{
  "Students": [
    {
      "id": 1,
      "Name": "Student1",
      "FName": "FName1",
      "Class": "I",
      "City": "Delhi"
    },
    {
      "id": 2,
      "Name": "Student2",
      "FName": "FName2",
      "Class": "II",
      "City": "Mumbai"
    },
     {
      "id": 3,
      "Name": "Student3",
      "FName": "FName3",
      "Class": "II",
      "City": "Delhi"
    },
    {
      "id": 4,
      "Name": "Student4",
      "FName": "FName4",
      "Class": "III",
      "City": "Mumbai"
    }
  ]
}

How can I get sub json list of students belongs to Delhi City?

Upvotes: 5

Views: 48449

Answers (5)

mfe
mfe

Reputation: 1208

You can use DSM library. By using it you can filter JSON while you reading JSON data.

Let's say you have POJO class for the student. Fields of student are not exactly matched with JSON data.

Student Class

public class Student {
    private long id;
    private String name;
    private String firstName;
    private String className;
    private String city;

 // getter/setter

}

You will define a yaml file that contains definitions of your mappings between json and Student class.

result:
  type: array     # result is list 
  path: /Students
  filter: self.data.city=='Delhi'   # filter by city field of class
  fields:
    id: long      # id is long and path is the same as json id field.
    name:
      path: Name
    firstName:
      path: FName
    className:
      path: Class
    city:
      path: City

Use DSM to filter JSON and deserialize to Student class

DSM dsm=new DSMBuilder(new File("student.yaml")).create(Student.class);
List<Student> students= (List<Student>)dsm.toObject(new File("student.json");

Here is students list that converted to json.

[ {
  "id" : 1,
  "name" : "Student1",
  "className" : "I",
  "city" : "Delhi",
  "firstName" : "FName1"
}, {
  "id" : 3,
  "name" : "Student3",
  "className" : "II",
  "city" : "Delhi",
  "firstName" : "FName3"
} ]

Upvotes: 0

Aaryn Tonita
Aaryn Tonita

Reputation: 490

If you are using Java 8 the following works (note: I am using Jackson, but as long as your JSON library returns a Map object for JSON objects, this example will still work):

// These 2 lines are Jackson specific    
ObjectMapper mapper = new ObjectMapper();
Map obj = mapper.readValue(s, Map.class);

List<Object> students = (List<Object>) obj.get("Students");
Object[] delhiStudents = students
    .stream()
    .filter(student -> ((Map)student).get("City").equals("Delhi"))
    .toArray();

Upvotes: 4

Alexis C.
Alexis C.

Reputation: 93842

If you don't mind using a third-party library, you can use GSON. What you have to do is providing a custom deserializer to the Gson object. This deserializer will exclude all the Students objects that have the specific city value in their JSON representation.

public class Test {
    public static void main(String[] args) throws Exception  {
        Type type = new TypeToken<List<Student>>() {}.getType();

        Gson gson = new GsonBuilder().registerTypeAdapter(type, new Student.StudentListDeserializer("Mumbai")).create();
        List<Student> list = gson.fromJson(new FileReader("myJson.json"), type);
        System.out.println(list);
    }
}

class Student {
    private long id;
    private String Name;
    private String FName;
    private String Class;
    private String City;


    @Override
    public String toString() {
        return "Student [id=" + id + ", Name=" + Name + ", FName=" + FName
                + ", Class=" + Class + ", City=" + City + "]";
    }

    static class StudentListDeserializer implements JsonDeserializer<List<Student>>{

        private Set<String> forbiddenCities;

        public StudentListDeserializer(String... forbiddenCities) {
            this.forbiddenCities = new HashSet<>(Arrays.asList(forbiddenCities));
        }

        @Override
        public List<Student> deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
            List<Student> list = new ArrayList<>();
            for(JsonElement e : json.getAsJsonObject().get("Students").getAsJsonArray()) {
                if(!forbiddenCities.contains(e.getAsJsonObject().get("City").getAsString())) {
                    list.add(context.deserialize(e, Student.class));
                }
            }
            return list;
         }               
     }        
}

which outputs:

[Student [id=1, Name=Student1, FName=FName1, Class=I, City=Delhi], Student [id=3, Name=Student3, FName=FName3, Class=II, City=Delhi]]

Upvotes: 0

G&#225;bor Bakos
G&#225;bor Bakos

Reputation: 9100

Using JSONPath the selection would look like this:

JsonPath.read(jsonAsString, "$.Students[?(@.City==Delhi)]")

Upvotes: 6

Ankur Singhal
Ankur Singhal

Reputation: 26067

you cannot play around much with available Json library, all you can do is to convert it into some POJO or say Map. refer this

Also i found something here as well. It might help you as well.

Upvotes: 0

Related Questions