json.set

This function sets the value of the existing key or add new key and set its value in a JSON object.

Syntax

json.set(jsonObject, key, value)

Arguments

Argument

Type

Description

jsonObject

any

The JSON object.

key

string

The key for value

value

any

The value for the key

Returns

Type

Description

any

The updated JSON object.

Examples

Let us say the jsonObject includes the following:

jsonObject = {
"colors": [
{ "id": 1, "value": "red" },
{ "id": 2, "value": "green" },
{ "id": 3, "value": "blue" }
]
}

The function json.set(jsonObject, "timestamp", 1652187627) returns the following: 

{
"colors": [
{ "id": 1, "value": "red" },
{ "id": 2, "value": "green" },
{ "id": 3, "value": "blue" }
],
"timestamp": 1652187627
}