json.set

From RPTools Wiki
Revision as of 18:57, 3 August 2009 by Khabalox (talk | contribs) (Added part about explicitly setting JSON Array variable to equal the result of the json.set to "permanently" store the change.)
Jump to navigation Jump to search

json.set() Function

Introduced in version 1.3b49
Sets the value in at the specified index in a JSON Array or for the specified key in a JSON Object. You can use an empty string ("") to represent a new JSON Array or JSON Object.

Usage

json.set(jarr, index, value)
json.set(jarr, index, value, ..., ...)
json.set(jobj, key, value)
json.set(jobj, key, value, ..., ...)

Parameters

  • jarr - The JSON Array that has an index's value set.
  • index - The numeric index which has its value set.
  • jobj - The JSON Object that has a key's value set.
  • key - The named key which has its value set.
  • value - The content that is set to the index or key.

Note: The index and value parameters or key and value parameters can be repeated in pairs.

Example

  [json.set("{}", "a", 5)]
  [json.set("", "a", 5, "b", 8)]
  [json.set(json.fromList("1,3"), 0, 8)]

Returns

 {"a":5}
 {"a":5, "b":8}
 [8, 3]

To save the new value in the JSON Array or JSON Object so that you can reference it later in a macro, you must set the array equal to the result of json.set().

   [myArray = json.append("", 1, 2, 3)]
   [myArray = json.set(myArray, 0, 5)]
   [myArray]

Returns

  [1,2,3] 
  [5,2,3] 
[5,2,3]

See Also