twilioliquidtwilio-studioliquid-templatetwilio-flow

Access value of json object in twilio studio flow


In a twilio studio flow I have a http request that returns json structured like this:

{"object": {"foo": {"bar": "test"}}}

I want to get the value of "bar".

I use a set variables widget with widgets.request.parsed.object and assign it to thing which works and gets me:

{"foo": {"bar": "test"}}

I then can't find a way to get bar.

I have tried:

flow.variables.thing.foo.bar

flow.variables.foo.bar

widgets.request.parsed.object.foo.bar

I have also tried turning on parse as JSON object with these.

It looks like it should be possibly but it doesn't work. The docs suggest that it should work: https://www.twilio.com/docs/studio/widget-library/set-variables

I want to avoid running a function if possible.


Solution

  • So, there are 2 ways to reference the value assigned to the key bar.

    The response is stored as flow data and can be referenced at any point in the flow; the reference you specified above {{widgets.request.parsed.object.foo.bar}} should work. You need to make sure that 'Parse as JSON Object' is not checked. As this will cause parse errors, these errors can be seen in the studio log steps.

    When using the set variables widget, you will want to use the to_json filter [1] and check the 'Parse as JSON Object' option when assigning a variable value i.e. {{widgets.request.parsed.object | to_json}}. If you assign this to the variable parsedObject, you can retrieve the bar value using the following reference {{widgets.set-variable-widget.parsedObject.foo.bar}}

    You can also set the response body as body in the set variable widget (and check the 'Parse as JSON Object' option), then you can also reference the value using the following reference {{widgets.set-variable-widget.body.object.foo.bar}}

    Hope this helps

    Rob

    [1] https://www.twilio.com/docs/studio/user-guide/liquid-template-language#standard-filters