0

I am new to Java programming and I am working on a Spring Boot application with a REST service which will call another service and return a JSON response. I am using OkHttpClient for handling this call.

However from the JSON response, I only require few attributes as final output in List format.

How can I extract only the required attributes from my okHttpCliwnt response ?

My response from the third party service looks like below :

{
    "employeeDetail": [{
            "employee": {
                "name": "abc",
                "age": "30",
                "details": {
                    "role": "developer",
                    "phone": "123"
                }
            }
        },
        {
            "employee": {
                "name": "abc",
                "age": "30",
                "details": {
                    "role": "sr.developer",
                    "phone": "1234"
                }
            }
        }
    ]
}

From this response, my final response needs to only be like below:

{
    "employeeDetail": [{
            "name": "abc",
            "age": "30",
            "role": "developer"
        },
        {
            "name": "abc",
            "age": "30",
            "role": "sr.developer"
        }
    ]
}

Please assist me.

Scarlett John
  • 99
  • 1
  • 11
  • Not a full answer, but if you look at the answer in https://stackoverflow.com/questions/28221555/how-does-okhttp-get-json-string he does what you're looking for in the code sample – user70585 Feb 23 '18 at 02:19

4 Answers4

0

I don't know how to convert JSON to List<> but you can convert JSON to Java object using Gson.

After that, you can add the contents of the object or the object itself to the list.

Here's a snippet from https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Gson gson = new Gson();

// 1. JSON to Java object, read it from a file.
Staff staff = gson.fromJson(new FileReader("D:\\file.json"), Staff.class);

// 2. JSON to Java object, read it from a Json String.
String jsonInString = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(jsonInString, Staff.class);

// JSON to JsonElement, convert to String later.
JsonElement json = gson.fromJson(new FileReader("D:\\file.json"), 
JsonElement.class);
String result = gson.toJson(json);
vanir
  • 349
  • 3
  • 11
0

I searched but for such nesting I couldn't find anything concrete. however I tried with JsonNode and I got to this.

    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(str);
    JsonNode empDetNode = rootNode.path("employeeDetail");
    Iterator<JsonNode> elements = empDetNode.elements();
    List<Employee> empList = new ArrayList<Employee>();
    Gson gson = new Gson();
    while (elements.hasNext()) {
        Employee emp1 = new Employee(); 
        JsonNode emp= elements.next();
        JsonNode empl= emp.path("employee");
        JsonNode name= empl.path("name");
        JsonNode age= empl.path("age");
        JsonNode details= empl.path("details");
        JsonNode role= details.path("details");
        emp1.setAge(age.toString());
        emp1.setName(name.toString());
        emp1.setRole(role.toString());
        empList.add(emp1);
    }
    EmpDetl empdetl = new EmpDetl();
    empdetl.setEmployeeDetl(empList);

Employee Class

public class Employee {
    private String name;
    private String age;
    private String role;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
}

EmployeeDetl

import java.util.List;

public class EmpDetl {

    private List<Employee> employeeDetl;
    public List<Employee> getEmployeeDetl() {
        return employeeDetl;
    }
    public void setEmployeeDetl(List<Employee> empLists) {
        this.employeeDetl = empLists;
    }
    @Override
    public String toString() {
        return "EmpDetl [empLists=" + employeeDetl + "]";
    }

}
Sagar Kharab
  • 369
  • 2
  • 18
0

Jackson might be the tool you are looking for. You just need to create a class, let's say Employee.java:

public class Employee {
    @JsonProperty("name")
    private String name;

    @JsonProperty("age")
    private String age;

    @JsonProperty("role")
    private String role;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
}

and EmployeeDetail.java

import java.util.List;

@JsonRootName(value = "employeeDetail")
public class EmployeeDetail {

    private List<Employee> employees;
    public List<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

More annotations, please refer

Bejond
  • 1,188
  • 1
  • 11
  • 18
-1

You have to do the parsing manually using org.json or some other json framework.

AtherSajjad
  • 109
  • 10