JSONSetElement
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 numeric value that specifies the type of data to be set (see below).
Data type returned
text
Originated in version
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 values. When value
is added into json
, the type
parameter specifies whether to convert value
to a specific JSON type or to insert value
without changing the type.
|
Input type for |
Output type |
|
FileMaker text |
JSON string (" ") |
|
FileMaker number |
JSON number |
|
JSON object |
JSON object ( { } ) |
|
JSON array |
JSON array ( [ ] ) |
|
FileMaker value or JSON Boolean value |
JSON Boolean |
|
Type is ignored |
JSON null |
|
JSON element |
JSON element (or JSON string, if |
-
You can specify
type
using either the named constant or the numeric value shown in parentheses—for example,JSONString
or 1—without quotes.
For JSONBoolean
, if value
is true
or a non-zero number, it is treated as true; if value
is false
or zero, it is treated as false. Otherwise, whether value
is true or false is determined in the same way as the test
parameter is in the If function.
For JSONRaw
, the JSON parser processes value
to determine whether it uses valid JSON syntax. If value
is valid JSON, the parsed result is used in this function's returned value without converting any elements to JSON data types. Any characters after the first JSON element in value
are ignored—for example, if value
is "4,2", only "4" will be inserted, because a comma is the separator between elements in JSON syntax. If value
is not valid JSON, value
is converted to a JSON string in the returned value.
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 ]
)
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
then
JSONFormatElements (
JSONSetElement ( $$JSON ; "b" ; "{ \"id\" : 14, \"lnk\" : 73 } " ;
JSONObject
)
)
returns
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
}