Skip to content
Reference > Methods

jsonExtract(json,xpath)

Definition

String jsonExtract(String json, String xpath)

Description

Walks through JSON to extract a particular value. If the value is a const then the string representation is returned, if the value is a data structure (list or map) then the value is returned as legal JSON (i.e. quotes are properly escaped). Returns null if (a) the supplied json is not legal JSON, (b) either supplied string is null, or (c) the xpath does not resolve to an existing value in the supplied JSON.

Parameter Definition

Name Type Description
json String Must be legal JSON, ex: 123 or
xpath String The path to supplied value, with each 'step' separated by period (.), To index into arrays use the zero based integer offset. To index into maps use the key value

Examples

1
2
3
4
5
String r1 = jsonExtract("\"simple\"",""); // r1 == simple
String r2 = jsonExtract("{a:1,b:2}","a"); // r2 == 1
String r3 = jsonExtract("{a:1,b:['rob','dave','steve\\'s']}","b"); // r3 == ['rob','dave','steve\'s']
String r4 = jsonExtract("{a:1,b:['rob','dave','steve\\'s']}","b.2"); // r4 == steve's
Object r5 = jsonExtract("{a:1,b:['rob','dave','steve\\'s',{e:1,f:2}]]},","b.2.f"); // r5 == null