Difference between revisions of "LlJsonGetValue"

From Second Life Wiki
Jump to: navigation, search
(Created page with "{{LSL Function |func_id=|func_sleep=0.0|func_energy=10.0 |func=llJsonGetValue|return_type=string|p1_type=string|p1_name=json|p2_type=list|p2_name=specifiers |func_footnote |func_…")
 
m
 
(38 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
{{LSL Function
 
{{LSL Function
 +
|inject-2={{Issues/BUG-3692}}
 
|func_id=|func_sleep=0.0|func_energy=10.0
 
|func_id=|func_sleep=0.0|func_energy=10.0
 
|func=llJsonGetValue|return_type=string|p1_type=string|p1_name=json|p2_type=list|p2_name=specifiers
 
|func=llJsonGetValue|return_type=string|p1_type=string|p1_name=json|p2_type=list|p2_name=specifiers
|func_footnote
 
 
|func_desc=Gets the value indicated by specifiers from the [http://json.org json] string.
 
|func_desc=Gets the value indicated by specifiers from the [http://json.org json] string.
|return_text=made by parsing '''json''', a string representing json and traversing as specified by specifiers.
+
|return_text=made by parsing '''json''', a string representing json and traversing as specified by '''specifiers'''.
 +
|func_footnote=When the input is invalid or no result can be found this function returns [[JSON_INVALID]]. If the result is <code>null</code> the function returns [[JSON_NULL]].
 
|spec=See [[Json_usage_in_LSL]]
 
|spec=See [[Json_usage_in_LSL]]
 
|constants
 
|constants
 +
|caveats=
 +
* [[llJsonGetValue]] is significantly slower than reading a value from a list with [[llList2String]]. But with [[llList2String]], you will need to parse your string and convert it to list. [[llJsonGetValue]] is significantly faster when you will need to parse your string: for instance by <code>[[llList2String]]([[llParseString2List]]( yourstring, [delimiters], [spacers]), N );</code> When you need to iterate intensively, think about [[llJson2List]].
 
|examples=
 
|examples=
 +
<source lang="lsl2">
 +
default {
 +
    state_entry() {
 +
   
 +
    //below is an example of a JSON=string with a key called "key" and a 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
 +
    }
 +
}
 +
 +
</source>
 +
<source lang="lsl2">
 +
JGetValTest(){
 +
    string j="[[1,2],[4,5,6]]";            //JSON may be written directly as a string like this in sl.
 +
    string k;                              //this will change with each command below;
 +
    k=llJsonGetValue(j,[]);                //returns the whole array of a JSON. It might just be one entry or a whole nested array or whole nested object.
 +
    //if "j" is a single JSON_STRING, this may return what the string represents as a JSON within a string; a JSON_NUMBER , JSON_TRUE, TRUE ...
 +
 +
    k=llJsonGetValue("\"3.14\"",[]);      //==k="3,14" (float that was stored in a JSON_STRING within a JSON. and not as JSON_NUMBER for no good reason)
 +
    k=llJsonGetValue("\"TRUE\""    ,[]);  //==k="TRUE" (the value was stored as a JSON_STRING and is thus returned verbatim)
 +
    k=llJsonGetValue(j,[0]);              //returns only the first entry (at offset 0). An entry can be any JSON type,
 +
                                          //each entry being separated by a comma from other entries.
 +
                                          //array and object entries may contain multiple comma separated entries within them.
 +
    k=llJsonGetValue(j,[1]);//returns only the second entry... (all the above still applies) k="[4,5,6]";
 +
    k=llJsonGetValue(llJsonGetValue(j,[1]),[2]);
 +
                                          //instead of getting an entry from "j" we get a sub-entry from llJsonGetValue(j,[1]),
 +
                                          //assuming the sub-entry is a JSON_ARRAY. It returns the 3rd sub-entry of the second entry,
 +
                                          //that is an array with 3 entries. it would make k="6".
 +
                                          //it will return JSON_INVALID if there is no 3rd entry in the 2nd sub-array,
 +
                                          //or no 2nd sub-array.
 +
    k=llJsonGetValue(j,[1,2]);            //Shorter way to do the same as in the previous example.
 +
    k=llJsonGetValue("true",[]);          //Sets k to JSON_TRUE
 +
    k=llJsonGetValue("True",[]);          //Sets k to JSON_INVALID because the JSON constant for 'true' is all lower case
 +
    k=llJsonGetValue("TRUE",[]);          //Sets k to JSON_INVALID because the JSON constant for 'true' is all lower case
 +
    k=llJsonGetValue(JSON_TRUE,[]);        //Sets k to JSON_INVALID. The JSON_xxxx constants are not supposed to be part of
 +
                                          // a JSON string, just values to test against.
 +
}
 +
</source>
 +
See also [[llJsonValueType]] for examples where the value type is checked before getting the value as that type.
 
|helpers
 
|helpers
 
|also_functions={{LSL DefineRow||[[llList2Json]]|}}
 
|also_functions={{LSL DefineRow||[[llList2Json]]|}}
{{LSL DefineRow||[[llJsonGetValue]]|}}
+
{{LSL DefineRow||[[llJson2List]]|}}
 
{{LSL DefineRow||[[llJsonSetValue]]|}}
 
{{LSL DefineRow||[[llJsonSetValue]]|}}
 
{{LSL DefineRow||[[llJsonValueType]]|}}
 
{{LSL DefineRow||[[llJsonValueType]]|}}
Line 19: Line 64:
 
|permission
 
|permission
 
|negative_index
 
|negative_index
|sort=CSV2List
+
|sort=JsonGetValue
 
|cat1=List
 
|cat1=List
 
|cat2=String
 
|cat2=String
 
|cat3=Data Conversion
 
|cat3=Data Conversion
|cat4=Json
+
|cat4=JSON
 +
|history = Date of Release  [[ Release_Notes/Second_Life_Server/13#13.05.20.276191 | 20/05/2013 ]]
 
}}
 
}}

Latest revision as of 09:50, 9 May 2015

Summary

Function: string llJsonGetValue( string json, list specifiers );

Gets the value indicated by specifiers from the json string.
Returns a string made by parsing json, a string representing json and traversing as specified by specifiers.

• string json
• list specifiers

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.

Specification

Caveats

  • llJsonGetValue is significantly slower than reading a value from a list with llList2String. But with llList2String, you will need to parse your string and convert it to list. llJsonGetValue is significantly faster when you will need to parse your string: for instance by llList2String(llParseString2List( yourstring, [delimiters], [spacers]), N ); When you need to iterate intensively, think about llJson2List.
All Issues ~ Search JIRA for related Bugs

Examples

default {
    state_entry() {
 
     //below is an example of a JSON=string with a key called "key" and a 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.
    string k;                              //this will change with each command below;
    k=llJsonGetValue(j,[]);                //returns the whole array of a JSON. It might just be one entry or a whole nested array or whole nested object.
    //if "j" is a single JSON_STRING, this may return what the string represents as a JSON within a string; a JSON_NUMBER , JSON_TRUE, TRUE ...
 
    k=llJsonGetValue("\"3.14\"",[]);       //==k="3,14" (float that was stored in a JSON_STRING within a JSON. and not as JSON_NUMBER for no good reason)
    k=llJsonGetValue("\"TRUE\""     ,[]);  //==k="TRUE" (the value was stored as a JSON_STRING and is thus returned verbatim)
    k=llJsonGetValue(j,[0]);               //returns only the first entry (at offset 0). An entry can be any JSON type,
                                           //each entry being separated by a comma from other entries.
                                           //array and object entries may contain multiple comma separated entries within them.
    k=llJsonGetValue(j,[1]);//returns only the second entry... (all the above still applies) k="[4,5,6]";
    k=llJsonGetValue(llJsonGetValue(j,[1]),[2]);
                                           //instead of getting an entry from "j" we get a sub-entry from llJsonGetValue(j,[1]), 
                                           //assuming the sub-entry is a JSON_ARRAY. It returns the 3rd sub-entry of the second entry, 
                                           //that is an array with 3 entries. it would make k="6". 
                                           //it will return JSON_INVALID if there is no 3rd entry in the 2nd sub-array,
                                           //or no 2nd sub-array.
    k=llJsonGetValue(j,[1,2]);             //Shorter way to do the same as in the previous example.
    k=llJsonGetValue("true",[]);           //Sets k to JSON_TRUE
    k=llJsonGetValue("True",[]);           //Sets k to JSON_INVALID because the JSON constant for 'true' is all lower case
    k=llJsonGetValue("TRUE",[]);           //Sets k to JSON_INVALID because the JSON constant for 'true' is all lower case
    k=llJsonGetValue(JSON_TRUE,[]);        //Sets k to JSON_INVALID. The JSON_xxxx constants are not supposed to be part of
                                           // a JSON string, just values to test against.
}
See also llJsonValueType for examples where the value type is checked before getting the value as that type.

See Also

Functions

•  llList2Json
•  llJson2List
•  llJsonSetValue
•  llJsonValueType

Articles

•  Typecast

Deep Notes

History

Date of Release 20/05/2013

All Issues

~ Search JIRA for related Issues
   JSON_NULL may be deceptively returned instead of JSON_INVALID when noncompliant Json text is encountered by either llJsonValueType or llJsonGetValue. Fixed with release 13.09.21.281328.

Signature

function string llJsonGetValue( string json, list specifiers );