Getting nested JSON float values

Given a JSON file formatted as shown in the screenshot below, how do I acces the individual float values using Processing JSONArray + JSONObject?

you can play with this for a start

(or post a short version of the json data as text)

JSONArray values;

void setup() {

  values = loadJSONArray("new.json");

  for (int i = 0; i < values.size(); i++) {

    JSONObject animal = values.getJSONObject(i); 

    JSONArray values2 = animal.getJSONArray("bboxes");

    JSONArray values3 = values2.getJSONArray(0);



    float id = values3.getFloat(0);


    println( id );
  }
}
2 Likes

Something like 0.008510975167155266 is double precision rather than just float: :warning:

What you’re requesting is pretty much what I’ve recently coded for this forum thread: :smile_cat:

Given you’ve already got the JSON file, there’s no need for the create & save util functions.

So we’re just gonna need jarr3dToArr3d() & joinArr3d() from the original sketch.

Basically the only thing I had to change was the statement below:
final JSONArray jarr2d = jarr3d.getJSONArray(z);

B/c the 2D double[][] array is inside a JSONObject w/ key “bboxes”:
final JSONArray jarr2d = jarr3d.getJSONObject(z).getJSONArray("bboxes");

It’d be easier if I just had your JSON file rather than type in the whole thing from your screenshot: :expressionless:

[
    {
        "filename": "000000000285.jpg",
        "bboxes": [
            [
                0.008510975167155266,
                0.3988480269908905,
                0.2383737415075302,
                0.615890383720398
            ],
            [
                0.45662403106689453,
                0.5138434171676636,
                0.5538952350616455,
                0.7475001811981201
            ]
        ]
    }
]

Anyways, here’s the adapted sketch which deals w/ your particular JSON file: :innocent:

/**
 * Conversion JSONArr3d to Arr3d (v1.0)
 * GoToLoop (2019/Dec/28)
 *
 * Discourse.processing.org/t/getting-nested-json-float-values/16674/3
 * Discourse.Processing.org/t/saving-storing-a-multidimensional-array/16391/5
 */

static final String FILENAME = "JArr3D.json", ENTRY = "bboxes";

double[][][] my3dArr;
JSONArray my3dJarr;

void setup() {
  my3dJarr = loadJSONArray(FILENAME);
  my3dArr = jarr3dToArr3d(my3dJarr);
  println(joinArr3d(my3dArr));
  exit();
}

static final double[][][] jarr3dToArr3d(final JSONArray jarr3d) {
  final int lenZ = jarr3d.size();
  final double[][][] arr3d = new double[lenZ][][];

  for (int z = 0; z < lenZ; ++z) {
    // final JSONArray jarr2d = jarr3d.getJSONArray(z);
    final JSONArray jarr2d = jarr3d.getJSONObject(z).getJSONArray(ENTRY);
    final int lenY = jarr2d.size();
    final double[][] arr2d = arr3d[z] = new double[lenY][];

    for (int y = 0; y < lenY; ++y)
      arr2d[y] = jarr2d.getJSONArray(y).getDoubleArray();
  }

  return arr3d;
}

static final String joinArr3d(final double[][][] arr3d) {
  final StringBuilder sb = new StringBuilder();
  int idxZ = 0;

  for (final double[][] arr2d : arr3d) {
    int idxY = 0;
    sb.append(ENTER);

    for (final double[] arr1d : arr2d) {
      int idxX = 0;

      for (final double d : arr1d)  sb
        .append('[').append(idxZ).append("][")
        .append(idxY).append("][").append(idxX++)
        .append("] ").append(d).append(ENTER);

      ++idxY;
    }

    ++idxZ;
  }

  return sb.toString();
}
4 Likes

Thanks so much @Chrisir & @GoToLoop :slight_smile:

2 Likes