# Iterate throw JSON array values

**URL:** <https://discourse.processing.org/t/iterate-throw-json-array-values/1710>\
**Category:** Coding Questions\
**Created:** [July 12, 2018, 7:39am UTC](https://discourse.processing.org/t/iterate-throw-json-array-values/1710 "2018-07-12T07:39:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [July 12, 2018, 7:39am UTC](https://discourse.processing.org/t/iterate-throw-json-array-values/1710/1 "2018-07-12T07:39:26Z")

</div>

Hello,  
I have those JSON data and I would like to iterate throw the lines who stars with `527157_FacetSep_`

```auto
{
  "response":{"numFound":1587807,"start":0,"docs":[]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "structHasAuthIdHal_fs":[
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Nathalie",47,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep_dario-r-varela-fernandez_FacetSep_Varela",13,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep_sylviane_FacetSep_Llinares",10,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Vu Thanh",7,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep_celine-borello_FacetSep_Borello",5,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Blaudeau ",3,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Niget ",3,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Pierre ",3,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Granger",2,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Hervé",2,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Sylviane",2,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Didier",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ François",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Yves",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Christophe",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Romain",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Valentin",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep Jörg",1,
        "527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Céline",0]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}}}

```

I made that code below, but it dont works… I get the array but I dont manage to get the string starting with `527157_FacetSep_` …

```auto
JSONObject json;

void setup() {
  json = loadJSONObject("data.json");
  JSONObject child = json.getJSONObject("facet_counts");
  JSONObject child2 = child.getJSONObject("facet_fields");
  JSONArray array = child2.getJSONArray("structHasAuthIdHal_fs");
  println(array.size());

  for (int i = 0; i< array.size(); i++) {
    // println(array.getInt(i));
    //JSONObject obj = array.getJSONObject(i);
    //println(obj)
   }
  exit();
}

```

If I compare with the [Processing getJSONArray() example](https://processing.org/reference/JSONArray_getJSONArray_.html) my data dont have the same format, no `:` and `{` for each lines…

thanks.

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [July 12, 2018, 1:04pm UTC](https://discourse.processing.org/t/iterate-throw-json-array-values/1710/2 "2018-07-12T13:04:48Z")

</div>

Hi mloiz!  
The issue is that the array you loaded has two types, integers and strings, stored in the array. You should load them using getInt() and getString(), and to solve the alternating key issue, just use mod. Here’s one way to do this with your code:

```auto
JSONObject json;

void setup() {
  json = loadJSONObject("data.json");
  JSONObject child = json.getJSONObject("facet_counts");
  JSONObject child2 = child.getJSONObject("facet_fields");
  JSONArray array = child2.getJSONArray("structHasAuthIdHal_fs");
  println(array.size());

  for (int i = 0; i< array.size(); i++) {
    if (i%2==0) {
      println(array.getString(i));
    } else {
      println(array.getInt(i));
    }
  }
  exit();
}

```

Also, try not to name things after common object names, like `array` for example, and try to keep your json properly indented to read.

---

<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:** [July 12, 2018, 1:20pm UTC](https://discourse.processing.org/t/iterate-throw-json-array-values/1710/3 "2018-07-12T13:20:26Z")

</div>

```auto
/**
 * JSON to CSV (v1.3)
 * GoToLoop (2018/Jul/12)
 * https://Discourse.Processing.org/t/iterate-throw-json-array-values/1710/3
 */

JSONObject json = loadJSONObject("data.json");
JSONObject facets = json.getJSONObject("facet_counts").getJSONObject("facet_fields");
JSONArray jsonArr = facets.getJSONArray("structHasAuthIdHal_fs");

Table t = new Table();
int[] colTypes = { Table.STRING, Table.STRING, Table.STRING, Table.INT };
t.setColumnTypes(colTypes);

for (int size = jsonArr.size(), i = 0; i < size; i += 2) {
  TableRow tr = t.addRow();
  String[] strings = trim(split(jsonArr.getString(i), ','));

  for (int j = 0; j < 3; tr.setString(j, strings[j++]));

  tr.setInt(3, jsonArr.getInt(i+1));
}

t.print();
saveTable(t, dataPath("data.csv"));

exit();

```

```auto
{
	"response": {
		"numFound": 1587807,
		"start": 0,
		"docs": []
	},
	"facet_counts": {
		"facet_queries": {},
		"facet_fields": {
			"structHasAuthIdHal_fs": [
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Nathalie",
				47,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep_dario-r-varela-fernandez_FacetSep_Varela",
				13,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep_sylviane_FacetSep_Llinares",
				10,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Vu Thanh",
				7,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep_celine-borello_FacetSep_Borello",
				5,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Blaudeau ",
				3,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Niget ",
				3,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Pierre ",
				3,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Granger",
				2,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Hervé",
				2,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Sylviane",
				2,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Didier",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ François",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Yves",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Christophe",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Romain",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_ Valentin",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep Jörg",
				1,
				"527157_FacetSep_Temps, Mondes, Sociétés_JoinSep__FacetSep_Céline",
				0
			]
		},
		"facet_ranges": {},
		"facet_intervals": {},
		"facet_heatmaps": {}
	}
}

```

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [July 15, 2018, 2:38pm UTC](https://discourse.processing.org/t/iterate-throw-json-array-values/1710/4 "2018-07-15T14:38:08Z")

</div>

thanks @WakeMeAtThree for the explanations,  
and thanks @GoToLoop for your example.
