Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

JSON.AddItemToObject

Adds a new entry to the object with the given name and value.

Component Version macOS Windows Linux Server iOS SDK
JSON 2.7 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "JSON.AddItemToObject"; json; Name...; json2... )   More

Parameters

Parameter Description Example
json A JSON text or reference. {"key":"value"}
Name... The name of the Object property. "length"
json2... The JSON text for the item. 50

Result

Returns JSON code.

Description

Adds a new entry to the object with the given name and value.

With version 6, you can pass several parameters to add several entries to the object.
When adding to a JSON reference, the function returns that reference number.

This function takes variable number of parameters. Pass as much parameters as needed separated by the semicolon in FileMaker.
Please repeat Name and json2 parameters as often as you need.

Examples

Build nested objects:

Set Variable [$j1; MBS( "JSON.CreateObject") ]
Set Variable [$j2; MBS( "JSON.AddStringToObject"; $j1; "name"; "" ) ]
Set Variable [$j3; MBS( "JSON.AddStringToObject"; $j2; "record_type"; "MX" ) ]
Display Dialog ["JSON"; $j3]
Set Variable [$j4; MBS( "JSON.CreateObject") ]
Set Variable [$j5; MBS( "JSON.AddItemToObject"; $j4; "record"; $j3 ) ]
Display Dialog ["JSON"; $j4]

Add two items to an object:

MBS( "JSON.AddItemToObject"; "{}"; "Values"; "[1,2]"; "Name"; "{\"FirstName\":\"Heinz\"}" )

Example result:
{ "Values": [1, 2], "Name": { "FirstName": "Heinz" } }

Add big number:

MBS( "JSON.AddItemToObject"; "{}"; "test"; "12063660878882855000013426248015578834577" )

Example result:
{   "test": 12063660878882855000013426248015578834577 }

Create JSON with values in fields on current layout:

Let ( [
    // Create object as reference
    j = MBS( "JSON.CreateObjectRef" );
    // List fields in current layout
    fields = FieldNames ( get(FileName) ; get(LayoutName ));
    // how many?
    c = ValueCount ( fields );
    // make a loop to add field values
    r = While ( i = 1 ; i ≤ c ; [ name = GetValue ( fields ; i ); r = MBS( "JSON.AddItemToObject"; j; name; MBS( "JSON.CreateValue"; GetField ( name ) )); i = i + 1 ] ; i );
    // format the result
    text = MBS( "JSON.Format"; j );
    // free json
    r = MBS( "JSON.Release"; j )
];
// return result as text
  text )

Example result:
{ "First": "John", "Last": "Miller", "Group": "", "Company": "Some Ltd.", "ID": 12 }

Build a JSON object with an array of objects in one Let:

let([

// build first object
o1 = "{}";
o1 = MBS("JSON.AddStringToObject"; o1; "FirstName"; "Joe");
o1 = MBS("JSON.AddStringToObject"; o1; "LastName"; "Miller");

// build first object
o2 = "{}";
o2 = MBS("JSON.AddStringToObject"; o2; "FirstName"; "Zoe");
o2 = MBS("JSON.AddStringToObject"; o2; "LastName"; "Jones");

// add them to array:
a1 = "[]";
a1 = MBS("JSON.AddItemToArray"; a1; o1);
a1 = MBS("JSON.AddItemToArray"; a1; o2);

// make result
r = "{}";
r = MBS( "JSON.AddItemToObject"; r; "people"; a1 );
r = MBS( "JSON.AddStringToObject"; r; "count"; 2 )
];r)

See also

Release notes

Example Databases

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 7th February 2023


JSON.AddItemToArray - JSON.AddNullToObject