Iterate throw JSON array values


#1

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

{
  "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_

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 my data dont have the same format, no : and { for each lines…

thanks.


#2

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:

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.


#3
/**
 * 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();
{
	"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": {}
	}
}

#4

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