Reference > Functions reference > Text functions > JSONSetElement
 
JSONSetElement
Purpose 
Adds or modifies an element in JSON data specified by an object name, an array index, or a path.
Format 
JSONSetElement(json;keyOrIndexOrPath;value;type)
Parameters 
json - any text expression or field that contains a JSON object or array.
keyOrIndexOrPath - any text expression or field that specifies a JSON object name (key), an array index, or a path. See Working with the JSON functions.
value - any expression or field that contains a number, text, or JSON data.
type - a named value that specifies the type of data to be set (see below).
Data type returned 
text
Originated in 
FileMaker Pro 16.0
Description 
This function returns json with value set at the specified keyOrIndexOrPath. If the json parameter is blank (""), this function adds value to a JSON object (in braces { }), unless the first part of the keyOrIndexOrPath parameter starts with a "[" character. In that case, this function adds value to a JSON array (in brackets [ ]).
For the type parameter, use one of the following named values to specify the type of data in the value parameter.
 
type
Specifies value is
JSONString
String (" ")
JSONNumber
Number
JSONObject
Object ( { } )
JSONArray
Array ( [ ] )
JSONBoolean
Boolean
JSONNull
Null
JSONRaw
To be determined by the JSON parser
For JSONBoolean, if value is true (in lowercase), it is treated as true. Otherwise, whether value is true or false is determined in the same way as the test parameter is in the If function.
For JSONRaw, value is processed by the JSON parser. If value is valid JSON data, the parsed result is used in this function’s returned value. Otherwise, value is used as a JSON string.
You can set multiple elements by providing an additional set of keyOrIndexOrPath, value, and type parameters in brackets [ ] for each element. The following syntax sets N elements at once:
JSONSetElement ( json ;
   [ keyOrIndexOrPath1 ; value1 ; type1 ] ;
   [ keyOrIndexOrPath2 ; value2 ; type2 ] ;
   ...
   [ keyOrIndexOrPathN ; valueN ; typeN ]
)
Notes 
This function is not supported in runtime solutions and returns "?".
Example 1 
Adds a key and its value to the root of a JSON object.
JSONSetElement ( "{ \"a\" : 11 }" ; "b" ; 22.23 ; JSONNumber ) returns {"a":11,"b":22.23}.
Example 2 
Adds a JSON object as an element of another JSON object. If the $$JSON variable is set to
{
"a" : {
"id" : 12,
"lnk" : 34
}
}
then
JSONFormatElements (
   JSONSetElement ( $$JSON ; "b" ; "{ \"id\" : 14, \"lnk\" : 73 } " ;
      JSONObject
   )
)
returns
{
"a" :
{
"id" : 12,
"lnk" : 34
},
"b" :
{
"id" : 14,
"link" : 73
}
}
Example 3 
In the Example JSON data stored in the $$JSON variable, changes the values of the "special" and "stock" keys in the first "product" element in the array.
JSONFormatElements (
   JSONSetElement ( $$JSON ;
   [ "bakery.product[0].special" ; 0 ; JSONBoolean ] ;
   [ "bakery.product[0].stock" ; 0 ; JSONNumber ]
   )
)
returns the same data as in $$JSON but with the first element of the "product" array changed to
{
"category" : "Breads",
"id" : "FB1",
"name" : "Donuts",
"price" : 1.99,
"special" : false,
"stock" : 0
}
Related topics 
Functions reference (category list)
Functions reference (alphabetical list)
About formulas
About functions
Defining calculation fields
Using operators in formulas
Working with the JSON functions