{ "country": [
{
"name": "USA",
"state": [
{
"name": "Arizona",
"city": [
{
"name" : "Phoenix",
"population": 5000000
} ,
{
"name" : "Tuscon",
"population": 1000000
}
]
} ,
{
"name": "California",
"city": [
{
"name" : "Los Angeles",
"population": 19000000
},
{
"name" : "Irvine"
}
]
}
]
} ,
{
"name": "Mexico",
"state": [
{
"name": "Veracruz",
"city": [
{
"name" : "Xalapa-Enr\u00edquez",
"population": 8000000
},
{
"name" : "C\u00F3rdoba",
"population": 220000
}
]
} ,
{
"name": "Sinaloa",
"city": [
{
"name" : "Culiac\u00E1n Rosales",
"population": 3000000
}
]
}
]
}
]
}
Copied!
begin-handler /parse-json public
// Read the JSON file
read-file "countries.json" to countries status st
if-true st lesser-equal 0
@Cannot read file or file empty
exit-handler -1
end-if
// Parse JSON
json-doc countries no-enum status st error-text et error-position ep to json
// Check for errors in JSON document
if-true st not-equal GG_OKAY
@Error [<<print-out et>>] at [<<print-out ep>>]
exit-handler -2
end-if
// This is the JSON element we're looking for
set-string city_name unquoted ="country"."state"."city"."name"
// Read elements one by one - note you can then store them in a tree or hash for future fast searches
start-loop
// Read just a key
read-json json key k type t
// Exit if end of document
if-true t equal GG_JSON_TYPE_NONE
break-loop
end-if
// If matches key we're looking for, get the value, and output it
if-true city_name equal k
read-json json value v
@Value [<<print-out v>>]
@--------
end-if
// Move on to the next JSON element
read-json json next
end-loop
// Optionally delete JSON object, or it will be automatically deleted
json-doc delete json
end-handler
Copied!
Content-Type: text/html;charset=utf-8
Cache-Control: max-age=0, no-cache
Pragma: no-cache
Status: 200 OK
Value [Phoenix]
--------
Value [Tuscon]
--------
Value [Los Angeles]
--------
Value [Irvine]
--------
Value [Xalapa-Enríquez]
--------
Value [Córdoba]
--------
Value [Culiacán Rosales]
Copied!