Home > Software design >  How to extract JSON fields from an api
How to extract JSON fields from an api

Time:04-08

I have an api which returns data in the below format when i use the clientbuilder get():

final Response response = ClientBuilder.newClient().target("url").queryParam("CustomerQuery", jsonarr).request(MediaType.APPLICATION_JSON).get();
String actual = response.readEntity(String.class);
System.out.println(actual);

Result:

{"_id":{"timestamp":1649320244,"date":"2022-04-07T08:30:44.000 00:00"},"ScheduleTime":"2022-04-07T09:50:00.000 00:00","History":[{"Status":"Pending","Time":"2022-04-07T08:30:44.011 00:00"}],"MyDetails":{"Query":"query1^^","name":"NEH","address":"XXX","Format":"xml","Version":"2"}}
{"_id":{"timestamp":1649320255,"date":"2022-04-07T08:30:55.000 00:00"},"ScheduleTime":"2022-04-07T09:50:00.000 00:00","History":[{"Status":"Pending","Time":"2022-04-07T08:30:55.011 00:00"}],"MyDetails":{"Query":"query2^^^","name":"ABC","address":"YYY","Format":"xml","Version":"1"}}

I need to extract fields under MyDetails in the above string and i tried using :

final Response response = ClientBuilder.newClient().target("url").request(MediaType.APPLICATION_JSON).get();
    JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
    System.out.println(jsonReader.readObject());

Please let me know how can i extract the fields.

CodePudding user response:

If you have questions about how to use a JsonObject, read the Javadoc https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
JsonObject o = jsonReader.readObject();
JsonObject details = o.getJsonObject("MyDetails");

// details.get...

Alternatively, use a different http client like Retrofit that encourages direct object mapping

CodePudding user response:

There are many ways to do what you want to do. I will just describe some of them. You can use several Json parsing libraries. The most popular ones are Json-Jackson also known as faster XML (See link here). You can use method readValue of ObjectMapper class. For class parameter you can use Map.class or your custom written class that will reflect the structure of your Json.

Than there is Gson library, with its user guide

But also if you want a simplistic solution, I wrote my own open-source library that includes JsonUtils class that is a thin wrapper over Json-Jackson library that gives you a very simple solution. In your case it may look like this (assuming variable json is a String that contains your Json string):

try {
  Map<String, Object>map = JsonUtils.readObjectFromJsonString(json, Map.class);
  Map details = map.get("MyDetails");
} catch(IOException ioe) {
...
}

Map details will contain a map with all your keys and values from section "MyDetails". If you want to use this library here is where to get it: Its called MgntUtils and you can get it on Github with Javadoc and source code. It is available as maven artifacts as well. Here is a Javadoc for JsonUtils class

  • Related