Operator Type:
Operator Scope of Action:
Operator Purpose:
Data Type Returned:
Operator First Added:
Operator in Current Baseline:
Operator Last Altered:
Function [other Function type actions]
Item [operators of similar scope]
Stream parsing [other Stream parsing operators]
source context dependent
v9.6.0
Baseline
As at baseline
JSON.json[keyStr]
If there is no current JSON object, attempts to parse the string as JSON and fails if unsuccessful. If there is a current JSON object, that object will be reused. keyStr is a quoted key name and [ ] square brackets, not ( ) parentheses must be used to pass keyStr. Be aware of possible operator name confusion as described in the Notes section further below.
If the top-level element is an object, Stream.json[key] returns a dictionary for that object. If the top-level element is an array, see JSON.json[itemNum].
For example if $Text is:
{ "title":"Becket", "price": 9.95 }
and $Subtitle is set to "title", then:
$Text.json["title"];
is "Becket".
$Text.json[title];
is "Becket".
$Text.json["price"];
is "9.95".
$Text.json['$Subtitle'];
no such field.
$Text.json[$Subtitle];
is "Becket".
Though multiple bracketed arguments can be used to address a JSON path, consider use of .jsonValue(pathStr) instead. For example:
Text: {
"person": { "firstName": "Thomas", "lastName": "Roe" },
"coordinates": [-90,41]
}
MyString: "person.lastName"
Then:
$Text.json['person']['lastName']
gives 'Roe'
But, easier:
$Text.json.jsonValue(person.lastName)
gives 'Roe'
$Text.json.jsonValue($MyString)
gives 'Roe' (from v9.6.0)
Again, using the same JSON as above:
$MyString="coordinates";
Now
$Text.json['coordinates'][0]
gives -90
(from v9.6.0)
$Text.json[$MyString][1]
gives 41
(from v9.6.0)
Notes
Be aware:
- If the source is $Text containing a mix of text and JSON first use .captureJson() to extract only the JSON code: see String.captureJson().
- Do not confuse this operator .json[] (note trailing square brackets) with the deprecated/legacy operator .json() (note trailing parentheses).
- The latter was used for export but is now replaced by String.jsonEncode().
- However, .json() does still work (legacy support) but is not the same as .json[] for reading JSON data as is described in this article.
See also—notes linking to here: