Working with the context Dictionary
Adding Key-Value pairs
You can add key-value pairs to the APIO context dictionary using the cell “Context setter”. Enter the data in the “Context setter” cell as a JSON Object, string or array, the value is automatically stored in the context dictionnary as a stringified version of the JSON, making it accessible for use in subsequent workflow steps.
1. JSON Objects
When a JSON object is provided in the context-setter cell, it is assigned to the specified key as a JSON string in the context dictionary.
As shown below, the value is stored as a JSON string.
{
"context":
{
"newKey": "{\n \"newValue\":\n {\n \"atttibute1\": \"value1\",\n \"attribute2\":\n {\n \"attribute21\": \"value21\",\n \"attribute22\": null,\n \"attribute23\": true\n }\n }\n}"
}
}
When storing the JSON string, you can also use the filter tojson that converts a dictionary into a JSON Object, this can be useful to compactify the JSON string. Instead of entering the data as a JSON object, you provide the data in the "Context Setter" cell as a Python dictionary.
For example,
In this example, the data is initialized as a dictionary to store the key-value pairs, and the expression {{data | tojson}}
converts the data dictionary into a JSON Object for output. As above, the resulting JSON object is automatically stored in the context dictionary as a JSON string, making it accessible for subsequent use in the workflow. The output is more compact than in the previous example.
{
"context":
{
"newKey": "{\"newValue\": {\"attribute2\": {\"attribute21\": \"value21\", \"attribute22\": null, \"attribute23\": true}, \"atttibute1\": \"value1\"}}"
}
}
Exercise
Create a new workflow with a single context-setter cell as described above. Click on the play button
Download the file by clicking on the button “Context as JSON”.
and analyse the content of the file.
2. Strings
If a string is specified in the context-setter cell (enclosed in double quotes: "newValue"
), it is wrapped in additional quotes in the context dictionary to preserve its type:
{
"context": {
"newKey": "\"newValue\""
}
3. Arrays
The context-setter cell supports arrays containing either strings or JSON objects. The array is stored in the context dictionary in a stringified format.
Array of Strings
Example input:
json["pnn1", "pnn2", "pnn3"]
Resulting context:
json{ "context": {"newKey": "[\"pnn1\", \"pnn2\", \"pnn3\"]"} }
Array of JSON Objects
Example input:
json[ {"id": 1, "value": "item1"}, {"id": 2, "value": "item2"} ]
Resulting context:
json{ "context": { "newKey": "[\n {\"id\": 1, \"value\": \"item1\"},\n {\"id\": 2, \"value\": \"item2\"}\n ]" } }
Using the | json
Filter for Conversion
To effectively use the values stored via the context-setter cell in templates, the | json
filter is essential. This filter ensures that the data is properly parsed into its intended format for use within the template.
- For JSON strings
If a key in context contains a JSON string (as in the exercise above), you can parse it into a dictionary using the | json filter. You can then access its attribute using dot notation:
Exercise
Use the content of the file downloaded in the previous section and copy/paste it in the Context panel of the Template Playground. Alternatively, copy/paste the following data in the Context panel
{
"context": {
"newKey": "{\"newValue\": {\"attribute1\": \"value1\", \"attribute2\": {\"attribute21\": \"value21\"}}}"
}
}
The key context.newKey contains a JSON string representing a nested structure. To parse the JSON string into a dictionary-like object, use the | json
filter::
{% set parsed_data = context.newKey | json -%}
After parsing, parsed_data
can be accessed like a typical dictionary: to extract specific attributes from the parsed dictionary, use dot notation:
Paste the following into the Template Panel:
{% set parsed_data = context.newKey | json -%}
Attribute 1: {{ parsed_data.newValue.attribute1 }}
Attribute 21: {{ parsed_data.newValue.attribute2.attribute21 }}
When you click "Run", the output will be:
Attribute 1: value1
Attribute 21: value21
For a more concise approach, you can directly apply the | json
filter and access attributes in one step without assigning an intermediate variable.
Attribute 1: {{ (context.newKey | json).newValue.attribute1 }}
Attribute 21: {{ (context.newKey | json).newValue.attribute2.attribute21 }}
For Strings
The double-quoted string wrapped in additional quotes in the context dictionary (e.g.,"\"newValue\""
) is transformed into a raw string.Template example:
jinja{{ context.newKey | json }}
Output:
newValue
For Arrays
For both arrays of strings and arrays of JSON objects, the| json
filter deserializes the stringified version stored in the context dictionary into its valid JSON representation. This ensures that the data, whether simple strings or complex objects, can be seamlessly used within the template- Array of Strings
This section builds on the example above when the array of strings
["pnn1", "pnn2", "pnn3"]
is specified in the context-setter cell:Template example:
jinja{{ context.newKey | json }}
Output:
["pnn1", "pnn2", "pnn3"]
- Array of JSON Objects
This section extends the example outlined earlier when the array of JSON objects
[{"id": 1, "value": "item1"},{"id": 2, "value": "item2"}]
is provided in the context-setter cell:Template example:
jinja{{ context.newKey | json }}
Output:
json[ {"id": 1, "value": "item1"}, {"id": 2, "value": "item2"} ]