Home » Java » parsing using jsonpath 3.0

parsing using jsonpath 3.0

Posted by: admin August 10, 2018 Leave a comment

Questions:

I have a below Json response from a query

    "data":    {
"hits":       [
{
"submitter_id": "TCGA-AB-2952",
"case_id": "300fa3a7-3c42-4edc-8992-400188e762b4",
"id": "300fa3a7-3c42-4edc-8992-400188e762b4",
"samples":             [
          {
"submitter_id": "TCGA-AB-2952-03A",
"sample_type": "Primary Blood Derived Cancer - Peripheral Blood",
"sample_id": "fdc2ed03-4ca8-432e-9b26-6b92049fa8c8"
},
          {
"submitter_id": "TCGA-AB-2952-11A",
"sample_type": "Solid Tissue Normal",
"sample_id": "5aa70bb8-e874-46b1-ba13-c3acf74a7fd9"
},
          {
"submitter_id": "TCGA-AB-2952-03B",
"sample_type": "Primary Blood Derived Cancer - Peripheral Blood",
"sample_id": "cef4ecb4-bd16-46dd-8719-a8e921abcda1"
}
],
"disease_type": "Acute Myeloid Leukemia"
},
{
"submitter_id": "TCGA-AB-2977",
"case_id": "f95063bc-a640-4980-99a7-84645902daf4",
"id": "f95063bc-a640-4980-99a7-84645902daf4",
"samples":             [
          {
"submitter_id": "TCGA-AB-2977-11A",
"sample_type": "Solid Tissue Normal",
"sample_id": "404143f6-bcb4-4be8-a791-6da7f09190ac"
},
          {
"submitter_id": "TCGA-AB-2977-03A",
"sample_type": "Primary Blood Derived Cancer - Peripheral Blood",
"sample_id": "87a5e85b-00c3-4eb7-9548-f2b83f6afac3"
},
          {
"submitter_id": "TCGA-AB-2977-03B",
"sample_type": "Primary Blood Derived Cancer - Peripheral Blood",
"sample_id": "ad885e3c-244e-42e7-9189-6d500e91ada6"
}
],
"disease_type": "Acute Myeloid Leukemia"
},
{
"submitter_id": "TCGA-AB-2889",
"case_id": "46692ac0-8993-4cd3-9590-32ec9dd2c802",
"id": "46692ac0-8993-4cd3-9590-32ec9dd2c802",
"samples":             [
          {
"submitter_id": "TCGA-AB-2889-11A",
"sample_type": "Solid Tissue Normal",
"sample_id": "99d6cb42-cc99-4ae0-bc8a-a40290286059"
},
          {
"submitter_id": "TCGA-AB-2889-03A",
"sample_type": "Primary Blood Derived Cancer - Peripheral Blood",
"sample_id": "1559866c-c7c6-4595-b5c5-559b7156ea9f"
}
],
"disease_type": "Acute Myeloid Leukemia"
},
{
"submitter_id": "TCGA-AB-2891",
"case_id": "79378590-e28e-403b-bbde-c76c0195a980",
"id": "79378590-e28e-403b-bbde-c76c0195a980",
"samples":             [
          {
"submitter_id": "TCGA-AB-2891-11A",
"sample_type": "Solid Tissue Normal",
"sample_id": "2bd32315-3174-41e5-8b49-b50e4ec7b53d"
},
          {
"submitter_id": "TCGA-AB-2891-03A",
"sample_type": "Primary Blood Derived Cancer - Peripheral Blood",
"sample_id": "1c8df1f1-f42d-4dca-a998-d0ae6ce58a22"
}
],
"disease_type": "Acute Myeloid Leukemia"
}
],
"pagination":       {
"count": 10,
"sort": "",
"from": 0,
"page": 1,
"total": 200,
"pages": 20,
"size": 10
}
},
"warnings": {}

I am trying to use json path 3.0.0 for parsing details from it following below documentation
https://static.javadoc.io/io.rest-assured/json-path/3.0.0/io/restassured/path/json/JsonPath.html

I can get and easily iterate through List cases = JsonPath.from(line).get(“data.hits.case_id”);

            List<String> diseases = JsonPath.from(line).get("data.hits.disease_type");

but for samples I am not able to properly Iterate.

I used

List<Map> Samples= JsonPath.from(line).get("data.hits.samples.findAll 
{samples -> samples.sample_id}");

But it is returning whole List>>

Is there any simplified way I can iterate through samples.I need to store all the samples(sample_id,submitter_id,sample_type) for each case_id

Answers: