Tinderbox v9 Icon

JSON.each([pathStr]){actions}


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Last Altered: 

 Function  [other Function type actions]

 List  [operators of similar scope]

 Stream parsing  [other Stream parsing operators]

 Baseline

 9.6.0


JSON.each([pathStr]){action(s)}

JSON.each{action(s)}

If the top-level element is an array, rebinds the JSON object in turn to each array element. After calling the action block for each element, the JSON object is restored.

For example, if the value of $MyString is [{"price":1}, {"price",2}], then

	$MyList=[];
	$MyString.json.each{$MyList += json["price"]*3;}

would set $MyList to "3;6".

JSON.each(pathStr){action(s)}

In .json.each(){…}, an optional path argument, pathStr, supplies a path to the array to be iterated. For example, if $Text is:

{  
   "person": { "firstName": "Thomas",  lastName: "Roe"},
   "coordinates" : [-90,41] 
}

then $Text.captureJSON().json.each(coordinates){…} would iterate through the array of coordinates.

If String.json.each begins a statement,

$Text.json.each(coordinates){...} 

.json.each(){} reuses the current JSON object. This can be much faster than repeatedly re-parsing a complex json package.

As a result, the older syntax $Text.json[coordinates].each(x){…}, that chained of JSON.json[keyStr] is no longer supported.

Loading a dictionary of dictionaries from JSON and looking up items

Let $Text be:

{ "French":{ "child":"enfant"; "cat":"chat"}; "Swedish":{"child":"barn";"cat":"katt"} } 

Now:

$Text.json[French][cat] is "chat"

$Text.json["Swedish"]["cat"] is "katt"

$Text.json.keys is "French; Swedish"

$Text.json.keys.each(x){ 
	$MyList=$MyList+$Text.json[x][cat];
};

$MyList is [chat;katt].

Loading a list of dictionaries from JSON a and looking up items

Let $Text be:

[ { "child":"enfant"; "cat":"chat"} , {"child":"barn";"cat":"katt"} ] 

Now:

$Text.json[0][cat] is chat

$Text.json[0]['cat'] is katt

$Text.json.count is 2

$Text.json.each(x){
	$MyList += x[child];
}

$MyList is now [enfant;barn].