# Getting nested JSON float values

**URL:** https://discourse.processing.org/t/getting-nested-json-float-values/16674
**Category:** Coding Questions
**Created:** [December 28, 2019, 1:13pm UTC](https://discourse.processing.org/t/getting-nested-json-float-values/16674 "2019-12-28T13:13:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AndreasRef](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andreasref/32/133_2.png) [@AndreasRef](https://discourse.processing.org/u/AndreasRef)
#### Post date: [December 28, 2019, 1:13pm UTC](https://discourse.processing.org/t/getting-nested-json-float-values/16674/1 "2019-12-28T13:13:39Z")

</div>

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

 ![Screenshot 2019-12-28 at 14.03.28](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/02bdab7c0cc5ced355ca4b2a59add949b4be9e9a.png)

---

<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: [December 28, 2019, 1:31pm UTC](https://discourse.processing.org/t/getting-nested-json-float-values/16674/2 "2019-12-28T13:31:14Z")

</div>

you can play with this for a start

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

```auto
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 );
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 28, 2019, 2:08pm UTC](https://discourse.processing.org/t/getting-nested-json-float-values/16674/3 "2019-12-28T14:08:15Z")

</div>

> [@AndreasRef](#):
>
> , how do I access the individual `float` values using Processing JSONArray + JSONObject?

Something like 0.008510975167155266 is `double` precision rather than just `float`: ⚠

> **[double / Reference](https://processing.org/reference/double.html)**
>
> Datatype for floating-point numbers larger than those that can be stored in a float. A float is a 32-bit values that can be as large as 3.40282347E+38 and as low as -3.40282347E+38. A

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

> [@Saving/Storing a multidimensional array](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391/5):
>
> For completeness’ sake I’m posting the “finalized” version which adds the loading part: angel /\*\* \* Conversion Arr3d to JSONArr3d (v1.0) \* GoToLoop (2019/Dec/17) \* Discourse.Processing.org/t/saving-storing-a-multidimensional-array/16391/5 \*/ static final String FILENAME = "JArr3D.json", OPT = "compact"; static final int RANGE = 100, DIMS = 4; double[][][] my3dArr = generateArr3d(RANGE, DIMS); JSONArray my3dJarr; void setup() { my3dJarr = arr3dToJarr3d(my3dArr); println(my3dJarr.for…

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: 😑

```auto
[
    {
        "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: 😇

```auto
/**
 * 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();
}

```

---

<div class="post-metadata">

### Author: ![AndreasRef](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andreasref/32/133_2.png) [@AndreasRef](https://discourse.processing.org/u/AndreasRef)
#### Post date: [December 28, 2019, 5:13pm UTC](https://discourse.processing.org/t/getting-nested-json-float-values/16674/4 "2019-12-28T17:13:01Z")

</div>

Thanks so much @Chrisir & @GoToLoop 🙂
