# Adding an existing 2 dimensional array to a JSONObject

**URL:** <https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [February 1, 2024, 7:59am UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807 "2024-02-01T07:59:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![remka](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/remka/32/19300_2.png) [@remka](https://discourse.processing.org/u/remka)\
**Post date:** [February 1, 2024, 7:59am UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807/1 "2024-02-01T07:59:36Z")

</div>

Hi everyone,

I am fairly new to Java and Processing, …coming from Javascript many things still look like black magic to me 🙂 …  
I am trying to format some data to JSON to send via websockets to an esp32 board. In JS its very straighforward since well, JSON is Javascript…

Here is a simplified version of my code:

```auto
// Variables
JSONObject json;
String jsonString;
String [][] myHexFrame;
int fps;

// These are inside a function
json = new JSONObject(); // Create a new JSON object. OK.
json.setInt("fps", fps); // Add a simple integer. Works no problem.
// json.setJSONArray("colors", myHexFrame); <-- The 2nd parameter should be... a JSONArray?
// How can I convert a "non JSON" array to a JSON one?
// Should I loop trough all values and push them one by one?
// Is there some kind of map() function to do that?
jsonString = json.toString(); // Stringify the whole thing. OK
ws.sendMessage(jsonString); // ..then send it. OK

```

The _myHexFrame_ array was filled earlier in the code with hex values.

```auto
String [][] myHexFrame = [
  ['0x2C699C', '0x2B6C9D', ...], // 64 columns
  ['0x2C699C', '0x2B6C9D', ...],
  ...]; // And 64 rows

```

Ultimately I’d like to have a JSON object looking like this:

```auto
json = {
  "fps": 60,
  "myHexFrame": [
    ['0x2C699C', '0x2B6C9D', ...],
    ['0x2C699C', '0x2B6C9D', ...],
    ...],
  "aString": "Here comes a string",
  ...
}

```

Thanks a lot!

---

<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:** [February 1, 2024, 10:44am UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807/2 "2024-02-01T10:44:33Z")

</div>

Maybe this [JSONArray / Reference / Processing.org](https://processing.org/reference/JSONArray.html)

Also, a 2d array is probably an array of arrays

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [February 1, 2024, 6:30pm UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807/3 "2024-02-01T18:30:49Z")

</div>

Hi @remka,

Look here…

> **[setJSONArray() / Reference](https://processing.org/reference/JSONArray_setJSONArray_.html)**
>
> Sets the value of the JSONArray with the associated index value.

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 2, 2024, 1:17am UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807/4 "2024-02-02T01:17:24Z")

</div>

Hello @remka,

Example:

```auto
// https://www.geeksforgeeks.org/different-ways-to-declare-and-initialize-2-d-array-in-java/
// https://www.tutorialspoint.com/how-to-write-create-a-json-array-using-java
// https://stackoverflow.com/questions/30005939/how-to-put-object-into-jsonobject-properly
// https://www.javatpoint.com/program-to-remove-all-the-white-spaces-from-a-string

String[][] hexFrame = { 
                      {"0xA00000", "0xB00000", "0xC00000", "0xD00000"},
                      {"0x000000", "0x100000", "0x200000", "0x300000"}
                      };

JSONObject jsonObject = new JSONObject();

jsonObject.put("fps", 60);
jsonObject.put("myHexFrame", hexFrame);

println(jsonObject);
println();

String s = jsonObject.toString();
s = s.replaceAll("\\s+", "");
println(s);

saveJSONObject(jsonObject, "data/new.json");

```

Output:

```auto
"myHexFrame": [
    [
      "0xA00000",
      "0xB00000",
      "0xC00000",
      "0xD00000"
    ],
    [
      "0x000000",
      "0x100000",
      "0x200000",
      "0x300000"
    ]
  ],
  "fps": 60
}

{"myHexFrame":[["0xA00000","0xB00000","0xC00000","0xD00000"],["0x000000","0x100000","0x200000","0x300000"]],"fps":60}

```

`:)`

---

<div class="post-metadata">

**Author:** ![remka](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/remka/32/19300_2.png) [@remka](https://discourse.processing.org/u/remka)\
**Post date:** [February 2, 2024, 4:07am UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807/5 "2024-02-02T04:07:08Z")

</div>

Thanks @glv, put() was all I needed. Not sure why this is not listed on [this page](https://processing.org/reference/JSONArray.html).  
I guess I was a bit confused with the java syntax for arrays too ^^

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 2, 2024, 7:58am UTC](https://discourse.processing.org/t/adding-an-existing-2-dimensional-array-to-a-jsonobject/43807/6 "2024-02-02T07:58:32Z")

</div>

> [@remka](#):
>
> Thanks @glv, put() was all I needed. Not sure why this is not listed on [this page](https://processing.org/reference/JSONArray.html).

You are welcome!

The Javadoc’s have additional information:

_[https://processing.github.io/processing-javadocs/core/](https://processing.github.io/processing-javadocs/core/)_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/a/8a7c27a75a01f7058cc01c99e809e807fac86fd8.png)

You can also enable _Code completion with CTRL-space_ and will see it:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/6/16675f816c5059105d9179e3ac16f99b3cce39e3.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/1/01e072ea9dae96e538c13217f4d405c8b6b10ca3.png)

`:)`
