# JSON Objects Attribute sequence issue

**URL:** <https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700>\
**Category:** Coding Questions\
**Created:** [February 10, 2021, 8:42am UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700 "2021-02-10T08:42:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [February 10, 2021, 8:42am UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700/1 "2021-02-10T08:42:16Z")

</div>

```auto
JSONObject json;
JSONArray array;
JSONObject loc;

void setup() {
  size(800, 800);
  array = new JSONArray();
  json = new JSONObject();
}

void draw() {
}

void keyPressed() {  
  if (key =='s') {

    for (int i=0; i<3; i++) { 
      JSONObject newO = new JSONObject();
      loc = new JSONObject();
      add_Location(i);
      newO.setInt("ID", i);   
      newO.setString("Attribute 1", "D");
      newO.setString("Attribute 2", "G");
      newO.setInt("Attribute 3", 2);
      newO.setJSONObject("Attribute 4", loc);
      newO.setString("Attribute 5", "DSP");    
      array.setJSONObject(i, newO);
    }
    json.setJSONArray("Object-Array", array);
    saveJSONObject(json, "data/new.json");
  }
}

void add_Location(int i) {
  loc.setFloat(" X ", i * 202.4565);
  loc.setFloat(" Y ", i * 255.2689);
}

```

I apologize in advance if this is very trivial. I am trying to save json array file with attributes of each  
object in sequence. But they appear in random order in data file and not in the order they are added.  
Is there any way to get them in sequence?

---

<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 10, 2021, 10:05am UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700/2 "2021-02-10T10:05:32Z")

</div>

I suggest a new approach: maybe make a JSONArray first (before the for-loop) and add new0 in the for-loop to it?

Chrisir

---

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [February 10, 2021, 4:29pm UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700/3 "2021-02-10T16:29:20Z")

</div>

Sorry I don’t understand. I have implemented the same way you are suggesting.

---

<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 10, 2021, 7:09pm UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700/4 "2021-02-10T19:09:47Z")

</div>

Good point.

But when you look at [JSONArray \ Language (API) \ Processing 3+](https://www.processing.org/reference/JSONArray.html)

No ` json.setJSONArray("Object-Array", array);` is needed

---

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [February 11, 2021, 7:56am UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700/5 "2021-02-11T07:56:44Z")

</div>

Tried your method but sequence is still out of order. I used object as I wanted to name the array which is not possible if I am directly saving the array.

---

<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 11, 2021, 10:41am UTC](https://discourse.processing.org/t/json-objects-attribute-sequence-issue/27700/6 "2021-02-11T10:41:23Z")

</div>

Ah, you don’t mean the order of elements (objects) in the array (i=0, i=1, i=2) BUT the data **within** one object.

I can’t help you with that.

- But then, it doesn’t really matter what the order is.

When you read the json you can show the data however you want.

```auto
{"Object-Array": [
  {
    "Attribute 5": "DSP",
    "Attribute 4": {
      " Y ": 0,
      " X ": 0
    },
    "ID": 0,
    "Attribute 1": "D",
    "Attribute 3": 2,
    "Attribute 2": "G"
  },
  {
    "Attribute 5": "DSP",
    "Attribute 4": {
      " Y ": 255.26890563964844,
      " X ": 202.4564971923828
    },
    "ID": 1,
    "Attribute 1": "D",
    "Attribute 3": 2,
    "Attribute 2": "G"
  },
  {
    "Attribute 5": "DSP",
    "Attribute 4": {
      " Y ": 510.5378112792969,
      " X ": 404.9129943847656
    },
    "ID": 2,
    "Attribute 1": "D",
    "Attribute 3": 2,
    "Attribute 2": "G"
  }
]
}

```

* * *

**Sketch**

```auto

JSONObject json;

void setup() {
  size(800, 800);
}

void draw() {
  //
}

void keyPressed() {  
  if (key == 's') {
    JSONArray array;
    array = new JSONArray();
    json = new JSONObject();
    for (int i=0; i<3; i++) { 
      JSONObject newO = new JSONObject();

      newO.setInt("ID", i);   
      newO.setString("Attribute 1", "D");
      newO.setString("Attribute 2", "G");
      newO.setInt("Attribute 3", 2);
      newO.setJSONObject("Attribute 4", add_Location(i));
      newO.setString("Attribute 5", "DSP");    
      array.setJSONObject(i, newO);
    }//for
    json.setJSONArray("Object-Array", array);
    saveJSONObject(json, "data/new.json");
    println("done.");
  }//if
}//func

JSONObject add_Location(int i) {
  JSONObject loc = new JSONObject();

  loc.setFloat(" X ", i * 202.4565);
  loc.setFloat(" Y ", i * 255.2689);

  return loc;
}

```
