# How to map an object to JSON in Processing

**URL:** https://discourse.processing.org/t/how-to-map-an-object-to-json-in-processing/38432
**Category:** Coding Questions
**Created:** [August 20, 2022, 1:41pm UTC](https://discourse.processing.org/t/how-to-map-an-object-to-json-in-processing/38432 "2022-08-20T13:41:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rico](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Rico](https://discourse.processing.org/u/Rico)
#### Post date: [August 20, 2022, 1:41pm UTC](https://discourse.processing.org/t/how-to-map-an-object-to-json-in-processing/38432/1 "2022-08-20T13:41:30Z")

</div>

Hi all,

I’ve been looking into various ways of mapping object to json in Processing and I can’t seem to get anything to work.

I have tried GSON, Javax and Jackson. but all throw errors. Maybe I’m not loading all the dependencies I need.

Does anybody have a working example of object mapping to/from json in a processing sketch please?

---

<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: [August 20, 2022, 2:58pm UTC](https://discourse.processing.org/t/how-to-map-an-object-to-json-in-processing/38432/2 "2022-08-20T14:58:29Z")

</div>

My theory is that you have to copy the values from the class into json value by value

here is an example

```auto

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

JSONArray values;

void setup() {

  size(320, 320);

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

  saveJSONArray(values, "data/new.json");
}

// Sketch saves the following to a file called "new.json":
// [
// {
// "id": 0,
// "species": "Capra hircus",
// "name": "Goat"
// },
// {
// "id": 1,
// "species": "Panthera pardus",
// "name": "Leopard"
// },
// {
// "id": 2,
// "species": "Equus zebra",
// "name": "Zebra"
// }
// ]

```

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [August 27, 2022, 4:49pm UTC](https://discourse.processing.org/t/how-to-map-an-object-to-json-in-processing/38432/3 "2022-08-27T16:49:29Z")

</div>

This might have something useful: [# JSON - how to get heirarchical names from file.](https://forum.processing.org/two/discussion/21503/json-how-to-get-heirarchical-names-from-file.html)
