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

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

{
  "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
    },
1 Like

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
1 Like

Welcome to the forum by the way!

Here is a link: https://processing.org/reference/JSONObject.html

My Sketch:

// 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

3 Likes