# How do I save an array of integers to json object/array

**URL:** <https://discourse.processing.org/t/how-do-i-save-an-array-of-integers-to-json-object-array/30583>\
**Category:** Coding Questions\
**Created:** [June 8, 2021, 7:56pm UTC](https://discourse.processing.org/t/how-do-i-save-an-array-of-integers-to-json-object-array/30583 "2021-06-08T19:56:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerrikoio](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@gerrikoio](https://discourse.processing.org/u/gerrikoio)\
**Post date:** [June 8, 2021, 7:56pm UTC](https://discourse.processing.org/t/how-do-i-save-an-array-of-integers-to-json-object-array/30583/1 "2021-06-08T19:56:43Z")

</div>

If I had an array of integers, like so: `int array[] = {1,2,3,4,5};`

How would I convert this array to JSON so that it is in this format  
`{ "array" : [1, 2, 3, 4, 5] }`

I am not sure what’s best inside my Processing(Java) code.

The documentation is not clear to me as to how to do this.  
[https://processing.org/reference/JSONArray.html](https://processing.org/reference/JSONArray.html)  
[https://processing.org/reference/JSONObject.html](https://processing.org/reference/JSONObject.html)

If anyone can help, that’ll be most greatly appreciated.

Thanks

---

<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:** [June 8, 2021, 8:31pm UTC](https://discourse.processing.org/t/how-do-i-save-an-array-of-integers-to-json-object-array/30583/2 "2021-06-08T20:31:41Z")

</div>

[Processing.GitHub.io/processing-javadocs/core/processing/data/JSONArray.html#JSONArray-processing.data.IntList-](http://Processing.GitHub.io/processing-javadocs/core/processing/data/JSONArray.html#JSONArray-processing.data.IntList-)

```auto
int[] intsArray = { 3, 0, -1 };
println(str(intsArray));
println();

JSONArray intsJson = new JSONArray(new IntList(intsArray));
println(intsJson);

exit();

```

---

<div class="post-metadata">

**Author:** ![gerrikoio](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@gerrikoio](https://discourse.processing.org/u/gerrikoio)\
**Post date:** [June 9, 2021, 9:06am UTC](https://discourse.processing.org/t/how-do-i-save-an-array-of-integers-to-json-object-array/30583/3 "2021-06-09T09:06:16Z")

</div>

Thanks, that’s most helpful.

I like the way you used a new IntList within a new JSONArray - that’s a neat trick.

```auto
new JSONArray(new IntList(intsArray));

```
