LlJsonSetValue
Looking for the current API? Open the llJsonSetValue reference →
Wiki description
Returns, if successful, a new JSON text string which is
json with the value indicated by the specifiers list set to value.If unsuccessful (usually because of specifying an out of bounds array index) it returns JSON_INVALID.
An "out of bounds array index" is defined to be any Integer
specifiers greater than the length of an existing array at that level within the Json text or greater than 0 (zero) at a level an array doesn't exist.A special
specifiers, JSON_APPEND, is accepted which appends the value to the end of the array at the specifiers level. Care should be taken- if that level is not an array, the existing Value there will be overwritten and replaced with an array containing value at it's first (0) index.If an existing "Key" is
specifiers at that level, its Value will be overwritten by value unless value is the magic value JSON_DELETE. If a value does not exist at specifiers, a new Key:Value pair will be formed within the Json object. To delete an existing value at
specifiers, use JSON_DELETE as the value. Note it will not prune empty objects or arrays at higher levels.If
value is JSON_TRUE, JSON_FALSE or JSON_NULL, the Value set will be the bare words 'true', 'false' or 'null', respectively, at the specifiers location within json.Specification
Caveats
- llListReplaceList() is roughly 2.8x as fast in replacing a single value of a list than llJsonSetValue() is to replace a single value in a json.
:The length of the list/json are irrelevant here.
Examples
string TEST_STRING_JSON;
init()
{
TEST_STRING_JSON = "[9,\"<1,1,1>\",false,{\"A\":8,\"Z\":9}]";
// [9,"<1,1,1>",false,{"A":8,"Z":9}]
say("Original TEST_STRING_JSON: " + TEST_STRING_JSON);
}
run_json_test(string input)
{
string output;
// changing values within the json string
// change the first value in the array to 10
output = llJsonSetValue(input, [0], "10");
// [10,"<1,1,1>",false,{"A":8,"Z":9}]
say("( 1): " + output);
// change the third value in the array to 'true'
output = llJsonSetValue(input, [2], JSON_TRUE);
// [9,"<1,1,1>",true,{"A":8,"Z":9}]
say("( 2): " + output);
// change the value of "A" within the Json object to 3
output = llJsonSetValue(input, [3, "A"], "3");
// [9,"<1,1,1>",false,{"A":3,"Z":9}]
say("( 3): " + output);
// adding a value or new key-value-pair within the input
// add the value "Hello" to the end of the array
// NOTE: One cannot insert, only add to the end
output = llJsonSetValue(input, [JSON_APPEND], "Hello");
// [9,"<1,1,1>",false,{"A":8,"Z":9},"Hello"]
say("( 4): " + output);
// add the key-value-pair "B":10 to the object
output = llJsonSetValue(input, [3, "B"], "10");
// [9,"<1,1,1>",false,{"A":8,"B":10,"Z":9}]
say("( 5): " + output);
// Things to look out for when modifying Json text
// ~!~ Be careful when using this function ~!~
// out of bounds array assignment:
// defined as attempting to add a value to a position ...
// ...greater than the length of the array (which may be 0)
// JSON_APPEND is ALWAYS the preferred way to add to an array
output = llJsonSetValue(input, [5], "10");
// %EF%B7%90 (URL escaped JSON_INVALID)
say("( 6): " + llEscapeURL(output));
// BUT, this works, since it is in bounds
// (eqivalent to JSON_APPEND in this case)
output = llJsonSetValue(input, [4], "10");
// [9,"<1,1,1>",false,{"A":8,"Z":9},10]
say("( 7): " + output);
// careless formation of new arrays
// ( the 4 and all subsequent 0's are all in bounds.)
output = llJsonSetValue(input, [4, 0, 0, 0], "10");
// [9,"<1,1,1>",false,{"A":8,"Z":9},[[[10]]]]
say("( 8): " + output);
// overwriting an object with an array:
// ~!~ mistaken use of JSON_APPEND on an object ~!~
output = llJsonSetValue(input, [3, JSON_APPEND], "10");
// [9,"<1,1,1>",false,[10]]
say("( 9): " + output);
// careless formation of new objects
// NOTE: "Key" assignemts will NEVER result in a return of JSON_INVALID!
output = llJsonSetValue(input, [3, "W", "X"], "10");
// [9,"<1,1,1>",false,{"A":8,"W":{"X":10},"Z":9}]
say("(10): " + output);
output = llJsonSetValue(input, [3, "W", "X", "Y"], "10");
// [9,"<1,1,1>",false,{"A":8,"W":{"X":{"Y":10}},"Z":9}]
say("(11): " + output);
// overwriting an array with an object
output = llJsonSetValue(input, ["X"], "10");
// {"X":10}
say("(12): " + output);
// special case considerations:
// BUG-3692: (NOTE: Corrected in release 13.09.21.281328!)
// a bug where, instead of JSON_INVALID being returned, if the out of
// bounds index is at a lower level than the topmost (root) level, a
// non-compliant JSON text would be formed
output = llJsonSetValue(input, [1, 7], "Disappearing Text");
// Note the "empty" second position that resulted in the returned array
// [9,,false,{"A":8,"Z":9}]
// (But now correctly shows JSON_INVALID)
say("(13): " + output);
// though there is no way to directly delete a key-value-pair
// nor remove a value from an array,
// the use of JSON_NULL may prove adequate
output = llJsonSetValue(input, [3, "A"], JSON_NULL);
// [9,"<1,1,1>",false,{"A":null,"Z":9}]
say("(14): " + output);
// if a JSON text object has been formed with llList2Json()
// that contains one or more duplicated "Keys", (allowable
// but NOT recommended!) ANY change
// made to that object will correct the condition,
// with all but the last such "Key" being removed
output = llList2Json(JSON_OBJECT, ["A", 1, "A", 2, "A", 3, "B", 4, "B", 4]);
// both Keys "A" and "B" are duplicated
// {"A":1,"A":2,"A":3,"B":4,"B":4}
say("(15): " + output);
// only the last value of the duplications is accessable though
// 3
say("(16): " + llJsonGetValue(output, ["A"]));
// condition corrected by adding a key-value-pair...
// {"A":3,"B":4,"Z":5}
say("(17): " + llJsonSetValue(output, ["Z"], "5"));
// ... or by changing a value
// {"A":5,"B":4}
say("(18): " + llJsonSetValue(output, ["A"], "5"));
}
say(string message)
{
llOwnerSay(message);
// llRegionSayTo(llGetOwner(), PUBLIC_CHANNEL, message);
// llWhisper(PUBLIC_CHANNEL, message);
}
default
{
on_rez(integer start_param)
{
llResetScript();
}
state_entry()
{
init();
}
touch_end(integer num_detected)
{
// copy 'TEST_STRING_JSON' from the following function call
// to the string 'input' in the function declaration
// and run a test on 'input' to not (!) modify 'TEST_STRING_JSON'
// but its copy instead
run_json_test(TEST_STRING_JSON);
}
}Double-quotes in string values are escaped. The following script inserts literal
string \"with\" quote instead of string "with" quotes as the JSON value.default
{
state_entry()
{
string test = "[\"a\"]";
string add = "string \"with\" quotes";
llOwnerSay(llJsonSetValue(test, [JSON_APPEND], add));
}
}See also: functions
See also: articles
History
Date of Release 20/05/2013
Shared wiki helpers
The original page also injects shared parameter notes, caveats or issue information through these helpers. Their conditional MediaWiki logic is not reproduced here; inspect the preserved helper source for additional material.
Original shared helper source (conditional wiki logic is not evaluated)
{{Issues|BUG-3692|type=bug|<s>[[JSON_NULL]] may be deceptively returned instead of [[JSON_INVALID]] when [http://tools.ietf.org/html/rfc4627 noncompliant Json text] is encountered by either [[llJsonValueType]] or [[llJsonGetValue]].</s> Fixed with release 13.09.21.281328.|dnc=*}}Template:LSL Function/negative index
Original shared helper source (conditional wiki logic is not evaluated)
{{LSL Injection Test}}<noinclude>
Format:
<nowiki>{{LSL Function/negative index|boolean|p1|p2|p3}}</nowiki><br/>
Exclude p1, p2, or p3 if not used.
{{#vardefine:also_articles|{{LSL DefineRow||Test}}}}
{{#vardefine:ni_mode|true}}
{{#vardefine:ni_nc|}}
{{#vardefine:ni_p1|number}}
{{#vardefine:ni_p2|p2}}
<!--{{#vardefine:ni_p3|p3}}-->
</noinclude>{{#if:
{{#vardefine:ni_c|{{#if:{{{2|{{#var:ni_p1}}}}}|{{#if:{{{3|{{#var:ni_p2}}}}}|{{#if:{{{4|{{#var:ni_p3}}}}}|3|2}}|1}}|0}}}}
{{#vardefine:t|{{#ifeq:{{{1|{{#var:ni_mode}}}}}|true|{{#switch:{{#var:ni_c}}|3|2=LSL_Function/negative_index/range|1=LSL_Function/negative_index/single}}}}}}
{{{{#var:t}}|{{{2|{{#var:ni_p1}}}}}|{{{3|{{#var:ni_p2}}}}}|{{{4|{{#var:ni_p3}}}}}|noExclude={{{noExclude|}}}}}
{{#if:{{{noSpec|}}}||
{{#vardefine:spec|{{#var:spec}}
{{#if:{{#var:t}}|
{{{!}} {{Prettytable|style=float:left;}}
{{!}}-{{Hl2}}
! Index
! Positive
! Negative
{{!}}-
{{!}} First
{{!}} <code>0</code>
{{!}} <code>-{{{length|length}}}</code>
{{!}}-
{{!}} Last
{{!}} <code>{{{length|length}}} - 1</code>
{{!}} <code>-1</code>
{{!}}}
=== Indexes ===
<div style="display:table;"><div style="display:block;">
*Positive indexes count from the beginning, the first item being indexed as <code>0</code>, the last as <code>({{{length|length}}} - 1)</code>.
</div></div>
<div style="display:table;"><div style="display:block;">
*Negative indexes count from the far end, the first item being indexed as <code>-{{{length|length}}}</code>, the last as <code>-1</code>.
</div></div>
}}
}}
}}
{{#vardefine:also_articles|{{#var:also_articles}}
{{#if:{{#var:t}}|{{LSL_DefineRow||{{LSLGC|Negative_Index{{#var:lang}}|Negative Index}}|}}}}}}
{{#vardefine:header_footnote|{{#var:header_footnote}}{{PBR}}
{{#vardefineecho:ni_ps|{{#switch:{{#var:ni_c}}
|0=This function
|1={{LSLP|{{{2}}}}}
|2={{LSLP|{{{2}}}}} & {{LSLP|{{{3}}}}}
|3={{LSLP|{{{2}}}}}, {{LSLP|{{{3}}}}} & {{LSLP|{{{4}}}}}
}}}} {{#if:{{#var:t}}| support{{#ifexpr:{{#var:ni_c}}>1||s}}| ''do{{#ifexpr:{{#var:ni_c}}>1||es}} not'' support }} {{LSLGC|Negative_Index{{#var:lang}}|negative indexes}}.{{PBR}}
}}
{{#vardefine:footer|{{#var:footer}}
{{#if:{{#var:t}}|{{#ifeq:{{NAMESPACE}}|Template||{{#if:{{#var:self}}{{{self|}}}{{#pos:{{#var:moded}}|r}}{{#pos:{{#var:moded}}|u}}||{{LSLC|Negative Index{{#var:lang}}}}}}}}|
{{#if:{{#var:self}}{{{self|}}}{{#pos:{{#var:moded}}|r}}{{#pos:{{#var:moded}}|u}}||{{LSLC|Positive_Index_Only{{#var:lang}}}}}}
}}}}
{{#vardefine:caveats|{{#var:caveats}}
{{#if:{{{nc|{{#var:ni_nc}}}}}||
* If {{#switch:{{#var:ni_c}}
|3=either {{LSLP|{{{2}}}}}, {{LSLP|{{{3}}}}} or {{LSLP|{{{4}}}}} are
|2=either {{LSLP|{{{2}}}}} or {{LSLP|{{{3}}}}} are
|1={{LSLP|{{{2}}}}} is}} out of bounds {{#if:{{{oob-return|}}}|this function returns {{{oob-return|}}} and}} the script continues to execute without an error message.
{{#if:{{#var:t}}|{{#ifexpr:{{#var:ni_c}}>1|{{#if:{{{noExclude|}}}|* {{LSLP|{{{2}}}}} & {{LSLP|{{{3}}}}} will not form an [[#exclusion_range|exclusion range]] when {{LSLP|{{{2}}}}} is past {{LSLP|{{{3}}}}} (Approximately: {{LSLP|{{{2}}}}} > {{LSLP|{{{3}}}}}), instead it will act as if {{LSLP|{{{2}}}}} was zero & {{LSLP|{{{3}}}}} was -1.|* {{LSLP|{{{2}}}}} & {{LSLP|{{{3}}}}} will form an [[#exclusion_range|exclusion range]] when {{LSLP|{{{2}}}}} is past {{LSLP|{{{3}}}}} (Approximately: {{LSLP|{{{2}}}}} > {{LSLP|{{{3}}}}}). }}}}}}
}}}}
}}<noinclude>
==Debugging==
{| {{Prettytable}}
|-{{Hl2}}
! #var
! value
|-
{{VarPair|header_footnote}}
|-
{{VarPair|spec}}
|-
{{VarPair|caveats}}
|-
{{VarPair|notes}}
|-
{{VarPair|constants_nb}}
|-
{{VarPairTable|also_articles}}
|-
{{VarPair|footer}}
|}
</noinclude>Original wiki source
Some wiki templates and tables need their original context. View this article on the Second Life Wiki. Technical wording and examples are retained from the source; historical guidance may differ from current behavior.