|
<< Click to Display Table of Contents >> Navigation: Several practical recommendations > Using Variables in Tasks > Working with JSON Data in RoboTask Variables |
Many RoboTask actions return data in JSON (JavaScript Object Notation) format. To process this information effectively, you need to know how to address specific fields or elements within a variable containing a JSON string.
RoboTask supports two primary methods for addressing JSON fields. Let’s look at them using a variable named SomeData containing the following structure:
{ |
Method 1: Dot Notation (Classical)
The most intuitive way to access data is by using a "dot" to separate levels of the hierarchy.
•Accessing an object: {json:SomeData.args.Messages} returns the entire array found in the Messages field.
•Accessing an array element: {json:SomeData.args.Messages[0]} returns the first object (index 0) of the Messages array.
•Deep nesting: {json:SomeData.args.Messages[0].To[0].Email} drills down to the email address of the first recipient in the first message.
Pros: Easy to read and write.
Cons: This method will fail if a field name contains special characters like spaces or dots (e.g., "User Name" or "item.id"), which are technically valid in JSON.
Method 2: Bracket Notation (Python-style)
To handle complex field names or to ensure maximum compatibility, you can use square brackets and quotes. This is the preferred method when dealing with REST APIs that might return "unfriendly" key names.
•Standard access: {json:SomeData["args"]["Messages"]}
•Array access: {json:SomeData["args"]["Messages"][0]}
•Deep nesting: {json:SomeData["args"]["Messages"][0]["To"][0]["Email"]}
Why use Bracket Notation?
In modern web services, you might encounter JSON keys like this: { "Service Status": "OK", "v1.0.attribute": "value" }
•Dot notation would break here because {json:Variable.Service Status} would be interpreted as a variable named Variable.Service followed by the text Status.
•Bracket notation handles it perfectly: {json:Variable["Service Status"]} or {Variable["v1.0.attribute"]}.
Comparison Table
Feature |
Dot Notation |
Bracket Notation |
Syntax Example |
Data.field |
Data["field"] |
Readability |
High |
Moderate |
Supports Spaces |
No |
Yes |
Supports Dots in Names |
No |
Yes |
Best Use Case |
Simple, standard JSON |
REST API responses, complex keys |
Related Topics
Saving of a result into a variable
Rules for using variables in expressions