An archive of community.esquilo.io as of Saturday January 26, 2019.

How to make Jsondecode reads value from list

triste

{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}

How to read "Open" inside list of menuitem from jsondecode ?

patrick
x <- jsondecode("{\"menu\": { \"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"} ] } }}")

print(x["menu"]["popup"]["menuitem"][0]["value"] + "\n")
New

print(x["menu"]["popup"]["menuitem"][1]["value"] + "\n")
Open
triste

Thank you so much, but how if multiple objects more than 100 in list?

patrick

I'm not sure what you're asking, but the list is indexable by integer, so you can iterate through it:

require("JSON");    
    
x <- jsondecode("{\"menu\": { \"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"} ] } }}")  
    
for (local i=0; i < x["menu"]["popup"]["menuitem"].len(); i++) {
    local value = x["menu"]["popup"]["menuitem"][i]["value"];
    print(value + "\n")    
    if (value == "Open")
      print ("Open index is " + i + "\n");
}
triste

{
"total_count": 2,
"items": [
{
"hap": "delivered",
"message": "Delivered: postmaster@sandbox28965d0043f240abc58e3f93a6af3683.mailgun.org \u2192 abc@gmail.com 'Hello'",
"type": "info",
"created_at": "Sat, 12 Mar 2016 21:05:10 GMT",
"message_id": "20160312210509.29601.69051@sandbox28965d0043f24049a58e3f93a6af3683.mailgun.org"
},
{
"hap": "accepted",
"message": "Accepted: postmaster@sandbox28965d0043f24049a58e3f93a6af3683.mailgun.org \u2192 abc@gmail.com 'Hello'",
"type": "info",
"created_at": "Sat, 12 Mar 2016 21:05:09 GMT",
"message_id": "20160312210509.29601.69051@sandbox28965d0043f24049a58e3f93a6af3683.mailgun.org"
}
]
}

I would like to get "message" from mailgun log with more than 50 items in the list, it is impossible to set the jsondecode manually in realtime. If there is any appropriate way to read the array item from json, something like JSONArray in JAVA.

patrick

it is impossible to set the jsondecode manually in realtime

Because it's too big and causes memory issues?

The built-in jsondecode library method only works on a full JSON string. You can't iterate over a subset to save memory. Maybe you could ask the API for a shorter list?

triste

Alright, how if the example above, how to set the value of "message id" in jsondecode format if the value is always not same.

patrick

Ok, you've kind of lost me.

But it case it's not clear "jsondecode format" is a standard Squirrel object. In this case it is a table that contains an array of "items". Each item is independently addressable by index (0 or 1, in this case). When you index either, you get a table object.

Each "item" object has a few key:value pairs, one of which has a key "message_id". The value of which can be set to whatever you want, independently of any other object in the array.