JSON.SetPathItem
----------------

Sets a value in a JSON hierarchy.

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

MBS( "JSON.SetPathItem"; JSON; Path; Value ) 

**MBS**( **"JSON.SetPathItem";** /\* Sets a value in a JSON hierarchy. \*/  
**$JSON**; /\* A JSON text or reference.e.g. "\[1,2,3\]" \*/   
**$Path**; /\* The path through the json hierarchy as list.  
For object, pass name of item to choose. For array pass index.  
For last array you can use special value "append" to add an entry.e.g. "people¶0¶last" \*/   
**$Value**) /\* The new value as JSON.e.g. "\\"Hello\\"" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| JSON | A JSON text or reference. | "\[1,2,3\]" |
| Path | The path through the json hierarchy as list.   For object, pass name of item to choose. For array pass index.   For last array you can use special value "append" to add an entry. | "people¶0¶last" |
| Value | The new value as JSON. | "\\"Hello\\"" |

### Result

Returns JSON or error.

### Description

Sets a value in a JSON hierarchy.  
The path describes the way through the hierarchy to find the JSON node to return.  
Please pass in new value as JSON.  
  
Version 9.2 automatically creates missing object/array entries. The value parameter will be treated as text if it is not a valid JSON instead of reporting an error.  
  
Implemented FileMaker syntax in v12.4 for JSON element get/set like in JSONSetElement function, so you can use dot notation, \[array index\] or quoted text in brackets.  
### Examples

Adds an entry with country:

 MBS ( "JSON.SetPathItem"; "{\\"people\\":\[{\\"first\\":\\"Christian\\",\\"last\\":\\"Schmitz\\",\\"city\\":\\"Nickenich\\"}\]}"; "people¶0¶country"; "\\"Germany\\"")  
<small>  
Example result:   
{ "people" : \[{ "first" : "Christian" , "last" : "Schmitz" , "city" : "Nickenich" , "country" : "Germany" }\] } </small>Sets an entry in an array:

 MBS ( "JSON.SetPathItem"; "\[1,2,3\]"; "1"; 5)  
<small>  
Example result:   
\[1 , 5 , 3 \] </small>Adds an entry to array in object:

 MBS ( "JSON.SetPathItem"; "{\\"test\\":\[1,2,3\]}"; "test¶append"; 5)  
<small>  
Example result:   
{ "test" : \[1 , 2 , 3 , 5 \] } </small>Sets hierarchical item:

 MBS ( "JSON.SetPathItem"; "{}"; "Hello¶World¶Test"; "Hello" )  
<small>  
Example result:   
{ "Hello" : { "World" : { "Test" : "Hello" } } } </small>Create missing array entries and add our new one:

 MBS ( "JSON.SetPathItem"; "\[\]"; "5"; "6th item" )  
<small>  
Example result: \[null, null, null, null, null, 6\]</small>Try FileMaker syntax to add value to object:

 MBS ( "JSON.SetPathItem"; "{ \\"a\\" : 11 }" ; "b" ; "22.23 " )  
<small>  
Example result:   
{ "a" : 11 , "b" : 22.23 } </small>Try FileMaker syntax to add value to array:

 MBS ( "JSON.SetPathItem"; "\[1,2,3\]"; "\[5\]"; 4)  
<small>  
Example result:   
\[1 , 2 , 3 , null, null, 4 \] </small>Try FileMaker syntax to set value in object in an array of an object:

 MBS ( "JSON.SetPathItem"; "{\\"people\\":\[{\\"first\\":\\"Christian\\",\\"last\\":\\"Schmitz\\",\\"city\\":\\"Nickenich\\"}\]}"; "people\[0\]last"; "test")  
<small>  
Example result:   
{ "people" : \[ { "first" : "Christian" , "last" : "test" , "city" : "Nickenich" } \] } </small>Try FileMaker syntax to add object to array:

 MBS ( "JSON.SetPathItem";   
"{\\"people\\":\[{\\"first\\":\\"Joe\\",\\"last\\":\\"Smith\\",\\"city\\":\\"San Francisco\\"}\]}";   
"people\[1\]";   
"{\\"first\\":\\"Peter\\",\\"last\\":\\"Miller\\",\\"city\\":\\"New York\\"}")  
<small>  
Example result:   
{ "people" : \[ { "first" : "Joe" , "last" : "Smith" , "city" : "San Francisco" }, { "first" : "Peter" , "last" : "Miller" , "city" : "New York" } \] } </small>### See also

- [FM.SQL.JSONRecords](FMSQLJSONRecords.md)
- [JSON.GetPathItem](JSONGetPathItem.md)

### Release notes

- **Version 14.3**
    - Improved [JSON.SetPathItem](https://www.mbsplugins.eu/JSONSetPathItem.shtml) to take the given value as text if it doesn't parse as JSON.
- **Version 13.0**
    - Changed [JSON.SetPathItem](https://www.mbsplugins.eu/JSONSetPathItem.shtml) to do nothing if value is empty (like v12.4 and before).
- **Version 12.5**
    - Fixed a regression in [JSON.GetPathItem](https://www.mbsplugins.eu/JSONGetPathItem.shtml) and [JSON.SetPathItem](https://www.mbsplugins.eu/JSONSetPathItem.shtml) to restore functionality, where you just grab an array item with just an index.
- **Version 12.4**
    - Improved [JSON.SetPathItem](https://www.mbsplugins.eu/JSONSetPathItem.shtml) and [JSON.GetPathItem](https://www.mbsplugins.eu/JSONGetPathItem.shtml) functions to accept FileMaker's style for object/array accessing.
- **Version 9.2**
    - Changed [JSON.SetPathItem](http://www.mbsplugins.eu/JSONSetPathItem.shtml) to create objects if they are missing.

### Example Databases

- [Files/FileOperation](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Files/FileOperation.shtml#7ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 14.3pr6](https://www.mbsplugins.de/archive/2024-07-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.6pr2](https://www.mbsplugins.de/archive/2022-12-07/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.5pr4](https://www.mbsplugins.de/archive/2022-10-24/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.4pr4](https://www.mbsplugins.de/archive/2022-08-30/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Recursive Folder Copy Script](https://www.mbsplugins.de/archive/2021-11-12/Recursive_Folder_Copy_Script/monkeybreadsoftware_blog_filemaker)
- [With JSON in excellent form](https://www.mbsplugins.de/archive/2019-11-16/With_JSON_in_excellent_form/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.2pr2](https://www.mbsplugins.de/archive/2019-04-05/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 5/2020, Seite 16](https://filemaker-magazin.de/neuigkeit/4110-Appetithappen-FMM_202005)
- [Ausgabe 4/2016, Seite 20](https://filemaker-magazin.de/neuigkeit/3876-Appetithappen-FMM_201604)

This function checks for a license.

Created 3th February 2016 , last changed 28th August 2022

  
[JSON.SetFormatOptions](JSONSetFormatOptions.md) - [JSON.SetVariables](JSONSetVariables.md)

[HTML Version](JSONSetPathItem.shtml)