Skip to content

llJsonGetValue

string llJsonGetValue(string JSON, list Specifiers)

Gets the value indicated by Specifiers from the JSON string.

Parameters
JSON (string)
Specifiers (list)
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
}
}
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)
}
  • 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 llUnescapeURL on 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 is null, the function returns JSON_NULL.
  • The function will convert the occurrence of \n to a line feed and \t to U+0009.
  • Performance note: As of Second Life Server 2024-06-11, llJsonGetValue takes approximately 2-3 ms per call regardless of depth, making it faster for reading a single element. However, llParseStringKeepNulls (5 ms) plus llList2String (0.5 ms per element) is faster when reading multiple elements from a dataset.
  • JSON does not correctly handle ] inside strings nested within arrays or } inside strings nested within objects (BUG-6495)