# Needed example of .toStringArray()

**URL:** <https://discourse.processing.org/t/needed-example-of-tostringarray/45653>\
**Category:** Beginners\
**Created:** [January 25, 2025, 3:59am UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653 "2025-01-25T03:59:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dneilan](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dneilan](https://discourse.processing.org/u/dneilan)\
**Post date:** [January 25, 2025, 3:59am UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/1 "2025-01-25T03:59:32Z")

</div>

> **[toStringArray() / Reference](https://processing.org/reference/jsonarray_tostringarray_)**
>
> Returns the entire JSONArray as an array of Strings. (All values in the array must be of the String type.)

I have tried evey which way to get this function to work…  
Anybody got a working example ?  
Here is my Json array: [{“id”: “1”}]  
I couldn’t make it any simpler. Or am I barking up the wrong tree ?

---

<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:** [January 25, 2025, 6:27am UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/2 "2025-01-25T06:27:27Z")

</div>

Its reference states: “(All values in the array must be of the **String** type.)”

However your [JSONArray](https://processing.org/reference/JSONArray.html) contains an [JSONObject](https://processing.org/reference/JSONObject.html) and not a string, which isn’t valid:  
`[{"id": "1"}]`

Should be instead something like this:  
`["id", "1"]`

---

<div class="post-metadata">

**Author:** ![dneilan](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dneilan](https://discourse.processing.org/u/dneilan)\
**Post date:** [January 25, 2025, 2:02pm UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/3 "2025-01-25T14:02:03Z")

</div>

## JSONArray loopData; loopData = loadJSONArray(“data.json”); println(loopData); String strngArray = loopData.toStringArray();

loads a JSON Array from file - containing [{“id”: “1”}]  
That is the minimum that the loadJSONArray will accept.  
This why I was asking for a working example.

---

<div class="post-metadata">

**Author:** ![dneilan](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dneilan](https://discourse.processing.org/u/dneilan)\
**Post date:** [January 25, 2025, 2:30pm UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/4 "2025-01-25T14:30:40Z")

</div>

Thanks for the response - [“id”,“id2”] does load correctly and IS converted to a String  
But that is Soooooo limited.  
I will have to do a manual conversion as my JSONArray contains Arrays, Objects etc.

---

<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:** [January 25, 2025, 8:54pm UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/5 "2025-01-25T20:54:48Z")

</div>

Hello @dneilan,

> [@dneilan](#):
>
> I will have to do a manual conversion as my JSONArray contains Arrays, Objects etc.

This is an interesting topic and I am interested in your manual conversion.  
A simple example will suffice.

This was a very quick effort for me and the data was straightforward from one of the Processing examples:

```auto
// Reference:
// https://www.freecodecamp.org/news/jsonobject-tostring-how-to-convert-json-to-a-string-in-java/

String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" };
String[] names = { "Goat", "Leopard", "Zebra" };

JSONArray values;

String [] JSONtoArr;

void setup() 
  {
  values = new JSONArray();

  for (int i = 0; i < species.length; i++) 
    {
    JSONObject animal = new JSONObject();

    animal.setInt("id", i);
    animal.setString("species", species[i]);
    animal.setString("name", names[i]);

    values.setJSONObject(i, animal);
    }
  
  String s = values.toString();
  println(s);
  println();
  
  JSONtoArr = split(s, '\n');
  printArray(JSONtoArr );
  println();
  
  for(int i=0; i< JSONtoArr.length; i++)
    {
    JSONtoArr[i] = JSONtoArr[i].trim();
    JSONtoArr[i] = JSONtoArr[i].replaceAll("\"", "");   
    }
  printArray(JSONtoArr);   
  println();
    
  saveStrings("test.txt", JSONtoArr);
  }

```

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

I always enjoy the challenge of custom parsing data to access elements and wield them with code.

Have fun!

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 26, 2025, 5:11am UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/6 "2025-01-26T05:11:40Z")

</div>

```auto
import java.util.Arrays;
  String[] words = {"go", "to ", "school"};
 println(Arrays.toString(words)); //print [go, to , school] 
 printArray(words);
//print
// [0] "go"
//[1] "to "
//[2] "school"
   

```

---

<div class="post-metadata">

**Author:** ![dneilan](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@dneilan](https://discourse.processing.org/u/dneilan)\
**Post date:** [January 26, 2025, 4:48pm UTC](https://discourse.processing.org/t/needed-example-of-tostringarray/45653/7 "2025-01-26T16:48:53Z")

</div>

Gee thanks. Not helpful
