Author Message
GChagnon
Joined: Jan 11, 2017
Messages: 34
Offline
Good afternoon,

I see that I can convert a JSON string into a JSON Object in Expression Editor by using the command JSON.parse(MyJsonString).

I have not found an example that explains how do I extract the value for "firstName" from the resulting JSON Object.

Thanks in advance,

Gabriel
AnuragAggarwal
Joined: Jun 1, 2014
Messages: 154
Offline
please use JSON.parse(MyJsonString).firstName
GChagnon
Joined: Jan 11, 2017
Messages: 34
Offline
Thank you - it works perfectly with this!!

JSON.parse(SmsEvent["message"].replace(/\(/g, "{").replace(/\)/g, "}").replace(/\+/g, " ")).firstName

We are sending JSON strings via Zang's SMS service. It unfortunately converts the curly brackets into parentheses and replaces each space in the body of the SMS message with a +. The above restores it all back into a JSON string that then can be parsed.

Easy peasy - not really! But thank the deity of your choice for Google for the precompiled expression that does the global replacement.

Question - in ED's Expression Editor, am I limited to a single expression or can stuff a couple of lines of code to do something more complicated that could say require a loop???

AnuragAggarwal
Joined: Jun 1, 2014
Messages: 154
Offline
currently only single expression, nothing more complicated on road map as well for now
GChagnon
Joined: Jan 11, 2017
Messages: 34
Offline
Good afternoon folks,

I have the following Object as a String in variable dfResponse["result"]:
"result": {
"source": "agent",
"resolvedQuery": "set blink pattern to 4",
"action": "SendIotCommand",
"actionIncomplete": false,
"parameters": {
"blinkPattern": "set blink pattern",
"number": 4
},
"contexts": [
{
"name": "patternnumber",
"parameters": {
"number": 4,
"blinkPattern": "set blink pattern",
"number.original": "4",
"blinkPattern.original": "blink pattern"
},
"lifespan": 1
}
],
"metadata": {
"intentId": "1570a9b0-534c-4a5a-bdfd-f433789d7aba",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "setBlinkPattern"
},
"fulfillment": {
"speech": "",
"messages": [
{
"type": 4,
"payload": {
"breeze": {
"userResponse": "Understood - IOT command has ben sent",
"ledSet": "4"
}
}
}
]
},
"score": 1
}

I can extract "action" String using the following:
JSON.parse(dfResponse["result"]).action

I can extract "fulfillment" Object using the following:
JSON.parse(dfResponse["result"]).fulfillment

But I cannot save it as a String as the Expression Editor just highlights the expression in red:
JSON.parse(dfResponse["result"]).fulfillment.toString()

I tried a bunch a variations with the same results. It is very possible that I got the expression incorrect but I think I may have pushed the Expression Editor one step too far.

I am trying to get the "breeze" Object and currently believe that I need to parse an Object out of "result", save it as a String and parse the saved Object again!

It would be easier if I could find an On-Line service into which I could paste my JSON Object and it provides the JSON properties for the Object. I could then use this in my Call REST Service as the Return Schema. I do not have the patience to do this by hand...

Thoughts,

Gabriel
GChagnon
Joined: Jan 11, 2017
Messages: 34
Offline
I can confirm that I was Googling the wrong sentence - it should have been JSON Schema not JSON properties...

The following works well if you remove all the options:
https://jsonschema.net/

Gabriel
AnuragAggarwal
Joined: Jun 1, 2014
Messages: 154
Offline
you are correct, this may be limitation of the expression editor not to allow

JSON.parse(dfResponse["result"]).fulfillment.toString()

we would remove this restriction in next release

in the meantime as workaround, you can create a variable with the schema, and map JSON.parse(dfResponse["result"]) to that variable and then you can use that variable further
AnuragAggarwal
Joined: Jun 1, 2014
Messages: 154
Offline
Can you please mention when you see the error - is it when

- you are trying to deploy the workflow and you see error in browser

- you are able to deploy ok, but you get error at runtime

can you also please attach the workflow
Go to:   
Mobile view