# How do you parse a nested JSON file like below using Processing JSON object?

**URL:** https://discourse.processing.org/t/how-do-you-parse-a-nested-json-file-like-below-using-processing-json-object/14053
**Category:** Coding Questions
**Created:** [September 18, 2019, 4:50pm UTC](https://discourse.processing.org/t/how-do-you-parse-a-nested-json-file-like-below-using-processing-json-object/14053 "2019-09-18T16:50:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![advancecoder](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/advancecoder/32/6082_2.png) [@advancecoder](https://discourse.processing.org/u/advancecoder)
#### Post date: [September 18, 2019, 4:50pm UTC](https://discourse.processing.org/t/how-do-you-parse-a-nested-json-file-like-below-using-processing-json-object/14053/1 "2019-09-18T16:50:28Z")

</div>

How do you parse a nested JSON file like below using Processing JSON object?

```auto
{
  "info": {
    "description": "COCO 2014 Dataset",
    "url": "http://cocodataset.org",
    "version": "1.0",
    "year": 2014,
    "contributor": "COCO Consortium",
    "date_created": "2017/09/01"
  },
  "images": [
    {
      "license": 5,
      "file_name": "COCO_train2014_000000057870.jpg",
      "coco_url": "http://images.cocodataset.org/train2014/COCO_train2014_000000057870.jpg",
      "height": 480,
      "width": 640,
      "date_captured": "2013-11-14 16:28:13",
      "flickr_url": "http://farm4.staticflickr.com/3153/2970773875_164f0c0b83_z.jpg",
      "id": 57870
    },

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 18, 2019, 6:10pm UTC](https://discourse.processing.org/t/how-do-you-parse-a-nested-json-file-like-below-using-processing-json-object/14053/2 "2019-09-18T18:10:09Z")

</div>

I don’t think it’s complete; it’s cut off I guess.

The entire thing is a jsonobject:

- Inside is an jsonobject followed by a
- jsonArray ( the [] brackets ), inside it (1 or many) jsonobjects

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 18, 2019, 6:18pm UTC](https://discourse.processing.org/t/how-do-you-parse-a-nested-json-file-like-below-using-processing-json-object/14053/3 "2019-09-18T18:18:17Z")

</div>

Welcome to the forum by the way!

Here is a link: [https://processing.org/reference/JSONObject.html](https://processing.org/reference/JSONObject.html)

My Sketch:

```auto
// load data from HD using loadJSONObject or use this test data variable with parseJSONObject
String data = 
  "{"+
  " \"info\": {"+
  " \"description\": \"COCO 2014 Dataset\","+
  " \"url\": \"http://cocodataset.org\","+
  " \"version\": \"1.0\","+
  " \"year\": 2014,"+
  " \"contributor\": \"COCO Consortium\","+
  " \"date_created\": \"2017/09/01\""+
  " },"+
  " \"images\": ["+
  " {"+
  " \"license\": 5,"+
  " \"file_name\": \"COCO_train2014_000000057870.jpg\","+
  " \"coco_url\": \"http://images.cocodataset.org/train2014/COCO_train2014_000000057870.jpg\","+
  " \"height\": 480,"+
  " \"width\": 640,"+
  " \"date_captured\": \"2013-11-14 16:28:13\","+
  " \"flickr_url\": \"http://farm4.staticflickr.com/3153/2970773875_164f0c0b83_z.jpg\","+
  " \"id\": 57870"+
  " } "+ // end of JSONObject
  " ] "+ // end of JSONArray
  "} "; // end of outer JSONObject

JSONObject json1; // the entire JSONObject 
JSONObject json2; // the first JSONObject inside it 

JSONArray jsonArr1; // the JSONArray inside it 

void setup() {
  size(1200, 550);

  json1 = parseJSONObject(data);

  if (json1 == null) {
    println("JSONObject could not be parsed");
    exit();
  } else {
    // get data 
    json2=json1.getJSONObject("info");
    jsonArr1=json1.getJSONArray("images");
  }
}

void draw() {
  background(70);

  // show some data from json2
  text(json2.getString("description"), 33, 33);
  text(json2.getString("url"), 33, 55);

  // for loop over jsonArr1
  for (int i = 0; i < jsonArr1.size(); i++) {

    // get JSONObject json3 at this position  
    JSONObject json3 = jsonArr1.getJSONObject(i);

    // show some data from json3
    text( "Image "
      + i 
      + ": "
      + json3.getInt("license")
      +"; "
      +json3.getString("file_name")
      +"; "
      +json3.getString("coco_url"), 
      133, 101 + i*22);
  }//for 
  //
}//func 
//

```

Regards, Chrisir
