<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Suparna Das</title>
    <description>The latest articles on DEV Community by Suparna Das (@suparnadas01).</description>
    <link>https://dev.to/suparnadas01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F489140%2F32b5e426-9781-434c-8575-afbcb7c6fb75.PNG</url>
      <title>DEV Community: Suparna Das</title>
      <link>https://dev.to/suparnadas01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suparnadas01"/>
    <language>en</language>
    <item>
      <title>How to parse complex Json response file in Java</title>
      <dc:creator>Suparna Das</dc:creator>
      <pubDate>Tue, 20 Oct 2020 16:59:06 +0000</pubDate>
      <link>https://dev.to/suparnadas01/how-to-parse-complex-json-response-file-in-java-3559</link>
      <guid>https://dev.to/suparnadas01/how-to-parse-complex-json-response-file-in-java-3559</guid>
      <description>&lt;p&gt;Parsing of JSON file may get bit complicated and confusing if you are not sure which library to use. There are different ways and Implementation available however I chose to use GSON library since it looked feasible for the JSON file which I was trying to parse. Sample JSON is provided below&lt;/p&gt;

&lt;p&gt;In this example, I have saved my response in a file for demonstration.&lt;br&gt;
Objective: Get the value of PatientID from this JSON&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "response": {
    "body": {
      "patients": {
        "patient": [
          {
            "patientID": "ALM22346789",
            "submittedStatus": "In Progress",
            "patientType": "Inpatient",
            "submissionDate": "2020-10-15"
          }
        ]
      }
    },
    "status": {
      "statusCode": "200",
      "statusMessage": {

      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Imports used:
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step1:&lt;/strong&gt;&lt;br&gt;
Read the file from the saved location and store it in JsonObject&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JsonObject myobject = (JsonObject) new JsonParser().parse(new FileReader("MyFilePath"));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step2:&lt;/strong&gt; Hold the "response" in a JSON Object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JsonObject response = (JsonObject) myobject.get("response");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step3:&lt;/strong&gt; Drill down further and store the "body" and "patient" in JSON Object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JsonObject body = (JsonObject) response.get("body");
JsonObject patients= (JsonObject) body.get("patients");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step4:&lt;/strong&gt; "patient" tag is in a Array hence we will store it in Array Object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JsonArray patientbody= (JsonObject) patients.get("patient");
JsonObject patient0= patientbody.get(0).getAsJsonObject();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step5:&lt;/strong&gt; Next we will fetch the "patientID"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String patientID =caseBody0.get("patientID").getAsString(); System.out.println(patientID);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OutPut: (as printed in console)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALM22346789
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
