JSON.AddItemToObject
--------------------

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

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [JSON](component_JSON.md) | [2.7](newinversion27.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "JSON.AddItemToObject"; JSON; Name...; json2... ) 

**MBS**( **"JSON.AddItemToObject";** /\* Adds a new entry to the object with the given name and value. \*/  
**$JSON**; /\* A JSON text or reference.e.g. {"key":"value"} \*/   
**$Name...**; /\* The name of the Object property.e.g. "length" \*/   
**$json2...**) /\* The JSON text for the item.e.g. 50 \*/ 

### 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](JSONCreateObject.md)") \]  
Set Variable \[$j2 ; MBS ( "[JSON.AddStringToObject](JSONAddStringToObject.md)"; $j1 ; "name"; "" ) \]  
Set Variable \[$j3 ; MBS ( "[JSON.AddStringToObject](JSONAddStringToObject.md)"; $j2 ; "record\_type"; "MX" ) \]  
Display Dialog \["JSON"; $j3 \]  
Set Variable \[$j4 ; MBS ( "[JSON.CreateObject](JSONCreateObject.md)") \]  
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\\"}" )  
<small>  
Example result:   
{ "Values" : \[1 , 2 \], "Name" : { "FirstName" : "Heinz" } } </small>Add big number:

 MBS ( "JSON.AddItemToObject"; "{}"; "test"; "12063660878882855000013426248015578834577" )  
<small>  
Example result:   
{ "test" : 12063660878882855000013426248015578834577 } </small>Create JSON with values in fields on current layout:

 Let ( \[  
 // Create object as reference  
 j = MBS ( "[JSON.CreateObjectRef](JSONCreateObjectRef.md)" );  
 // 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](JSONCreateValue.md)"; GetField ( name ) )); i = i + 1 \] ; i );  
 // format the result  
 text = MBS ( "[JSON.Format](JSONFormat.md)"; j );  
 // free json  
 r = MBS ( "[JSON.Release](JSONRelease.md)"; j )  
\];  
// return result as text  
 text )  
<small>  
Example result:   
{ "First" : "John" , "Last" : "Miller" , "Group" : "" , "Company" : "Some Ltd." , "ID" : 12 } </small>Build a JSON object with an array of objects in one Let:

 let(\[  
  
// build first object  
o1 = "{}";  
o1 = MBS ("[JSON.AddStringToObject](JSONAddStringToObject.md)"; o1; "FirstName"; "Joe");  
o1 = MBS ("[JSON.AddStringToObject](JSONAddStringToObject.md)"; o1; "LastName"; "Miller");  
  
// build first object  
o2 = "{}";  
o2 = MBS ("[JSON.AddStringToObject](JSONAddStringToObject.md)"; o2; "FirstName"; "Zoe");  
o2 = MBS ("[JSON.AddStringToObject](JSONAddStringToObject.md)"; o2; "LastName"; "Jones");  
  
// add them to array:  
a1 = "\[\]";  
a1 = MBS ("[JSON.AddItemToArray](JSONAddItemToArray.md)"; a1; o1);  
a1 = MBS ("[JSON.AddItemToArray](JSONAddItemToArray.md)"; a1; o2);  
  
// make result  
r = "{}";  
r = MBS ( "JSON.AddItemToObject"; r; "people"; a1 );  
r = MBS ( "[JSON.AddStringToObject](JSONAddStringToObject.md)"; r; "count"; 2 )   
\];r)  
### See also

- [JSON.AddNumberToObject](JSONAddNumberToObject.md)
- [JSON.AddStringToObject](JSONAddStringToObject.md)
- [JSON.AddTrueToObject](JSONAddTrueToObject.md)
- [JSON.CreateArrayRef](JSONCreateArrayRef.md)
- [JSON.CreateObjectRef](JSONCreateObjectRef.md)
- [JSON.CreateValue](JSONCreateValue.md)
- [JSON.Format](JSONFormat.md)
- [JSON.Release](JSONRelease.md)
- [JSON.ReplaceEvaluate](JSONReplaceEvaluate.md)
- [MapView.Snapshot](MapViewSnapshot.md)

### Release notes

- **Version 16.2**
    - Changed [JSON.AddValueToObject](https://www.mbsplugins.eu/JSONAddValueToObject.shtml), [JSON.AddItemToObject](https://www.mbsplugins.eu/JSONAddItemToObject.shtml) and [JSON.AddNumberToObject](https://www.mbsplugins.eu/JSONAddNumberToObject.shtml) to replace existing items instead of adding a duplicate.
- **Version 14.4**
    - Improved [JSON](https://www.mbsplugins.eu/component_JSON.shtml) functions to return error if you pass non JSON parameters to [JSON.AddItemToArray](https://www.mbsplugins.eu/JSONAddItemToArray.shtml), [JSON.AddItemToObject](https://www.mbsplugins.eu/JSONAddItemToObject.shtml), [JSON.Replace](https://www.mbsplugins.eu/JSONReplace.shtml) and related functions.

### Example Databases

- [JSON/JSON Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JSON/JSON%20Test.shtml#1ScriptAnchor_)
- [Mac and iOS/MapKit/MapKit Snapshots](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20and%20iOS/MapKit/MapKit%20Snapshots.shtml#1ScriptAnchor_)
- [Mac and iOS/MapKit/MapView BeeExample](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20and%20iOS/MapKit/MapView%20BeeExample.shtml#28ScriptAnchor_)
- [Mac and iOS/MapKit/MapView Click Polygons](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20and%20iOS/MapKit/MapView%20Click%20Polygons.shtml#12ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 16.2pr5](https://www.mbsplugins.de/archive/2026-05-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 14.4pr6](https://www.mbsplugins.de/archive/2024-09-11/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [JSON Replace with evaluate](https://www.mbsplugins.de/archive/2024-05-06/JSON_Replace_with_evaluate/monkeybreadsoftware_blog_filemaker)
- [FileMaker and MongoDB](https://www.mbsplugins.de/archive/2023-03-20/FileMaker_and_MongoDB/monkeybreadsoftware_blog_filemaker)
- [With JSON in excellent form](https://www.mbsplugins.de/archive/2019-11-16/With_JSON_in_excellent_form/monkeybreadsoftware_blog_filemaker)
- [Fun with While function in FileMaker](https://www.mbsplugins.de/archive/2019-07-22/Fun_with_While_function_in_Fil/monkeybreadsoftware_blog_filemaker)
- [JSON with large integers](https://www.mbsplugins.de/archive/2018-05-11/JSON_with_large_integers/monkeybreadsoftware_blog_filemaker)
- [Generate JSON with MBS Plugin](https://www.mbsplugins.de/archive/2013-12-03/Generate_JSON_with_MBS_Plugin/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 3/2022, Seite 27](https://filemaker-magazin.de/neuigkeit/4187-Appetithappen-FMM_202203)
- [Ausgabe 5/2020, Seite 17](https://filemaker-magazin.de/neuigkeit/4110-Appetithappen-FMM_202005)

This function checks for a license.

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

  
[JSON.AddItemToArray](JSONAddItemToArray.md) - [JSON.AddNullToObject](JSONAddNullToObject.md)

[HTML Version](JSONAddItemToObject.shtml)