llJsonGetValue
string llJsonGetValue(string JSON, list Specifiers)Gets the value indicated by Specifiers from the JSON string.
Parameters
-
JSON(string) -
Specifiers(list)
Basic Key-Value Lookup
Section titled “Basic Key-Value Lookup”default { state_entry() { // Example of a JSON string with a key called "key" and value "val" string json1 = "{\"key\":\"val\"}"; llSay(0, llJsonGetValue(json1, ["key"])); // returns "val" in localchat
string json2 = "{\"mansBestFriend\":\"dog\"}"; llSay(0, llJsonGetValue(json2, ["mansBestFriend"])); // returns "dog" in localchat }}Accessing Arrays and Nested Data
Section titled “Accessing Arrays and Nested Data”JGetValTest() { string j = "[[1,2],[4,5,6]]"; // JSON may be written directly as a string like this in SL
// Returns the whole array as a JSON string string k = llJsonGetValue(j, []);
// Get first element (index 0) k = llJsonGetValue(j, [0]); // returns "[1,2]"
// Get second element (index 1) k = llJsonGetValue(j, [1]); // returns "[4,5,6]"
// Access nested elements using chained specifiers k = llJsonGetValue(j, [1, 2]); // returns "6" (third element of second sub-array)
// JSON primitives and type conversion k = llJsonGetValue("\"3.14\"", []); // returns "3.14" (string value from JSON_STRING) k = llJsonGetValue("true", []); // returns JSON_TRUE (lowercase "true" is valid JSON boolean) k = llJsonGetValue("True", []); // returns JSON_INVALID (uppercase "True" is not valid JSON)}Caveats
Section titled “Caveats”- Manual definition of JSON syntax within LSL does not always parse as expected. Be careful to avoid using syntax characters anywhere other than where needed (for example, inside a string). Alternatively, escape the syntax character and use
llUnescapeURLon the retrieved string to convert it back. - When the input is invalid or no result can be found, this function returns
JSON_INVALID. If the result isnull, the function returnsJSON_NULL. - The function will convert the occurrence of
\nto a line feed and\ttoU+0009. - Performance note: As of Second Life Server 2024-06-11,
llJsonGetValuetakes approximately 2-3 ms per call regardless of depth, making it faster for reading a single element. However,llParseStringKeepNulls(5 ms) plusllList2String(0.5 ms per element) is faster when reading multiple elements from a dataset.
Known Issues
Section titled “Known Issues”- JSON does not correctly handle
]inside strings nested within arrays or}inside strings nested within objects (BUG-6495)