I have following object
JavaScript
x
21
21
1
{
2
"tenant": "bclient",
3
"name": "somename",
4
"published_at": "2010-01-01T12:00:00.000Z",
5
"payload": {
6
"id": "04d02325-f4ea-4a7b-bfeb-2ff74a0e1a0d",
7
"external_id": "849849889",
8
"created_at": "2018-07-06T11:56:34.712Z",
9
"placed_at": "2018-07-06T12:06:25.989Z",
10
"associate_id": "121edewcsecsdc",
11
"associate_email": "abc@example.com",
12
"channel_type": "web",
13
"channel": "webshop-123",
14
"is_exchange": false,
15
"customer_email": "johndoe@example.com",
16
"customer_id": "84ca4scac9aca98s",
17
"external_customer_id": "CUST123423",
18
"is_historical": true,
19
}
20
}
21
I want to remove is_exchange and replace value of created_at to “2021-03-23” and get back the json representation of the object
I tried like
JavaScript
1
3
1
var data= JSON.parse(myjsonobj)
2
delete data['payload']['external_id'];
3
which is working but
JavaScript
1
2
1
data.payload.created_at = '2021-03-23'
2
is not working
JavaScript
1
27
27
1
const myjsonobj = `{
2
"tenant": "bclient",
3
"name": "somename",
4
"published_at": "2010-01-01T12:00:00.000Z",
5
"payload": {
6
"id": "04d02325-f4ea-4a7b-bfeb-2ff74a0e1a0d",
7
"external_id": "849849889",
8
"created_at": "2018-07-06T11:56:34.712Z",
9
"placed_at": "2018-07-06T12:06:25.989Z",
10
"associate_id": "121edewcsecsdc",
11
"associate_email": "abc@example.com",
12
"channel_type": "web",
13
"channel": "webshop-123",
14
"is_exchange": false,
15
"customer_email": "johndoe@example.com",
16
"customer_id": "84ca4scac9aca98s",
17
"external_customer_id": "CUST123423",
18
"is_historical": true
19
}
20
}`
21
22
var data = JSON.parse(myjsonobj)
23
delete data['payload']['external_id'];
24
25
data.payload.created_at = '2021-03-23'
26
27
console.log(data)
Advertisement
Answer
To remove a value from an object use the delete
method. So, you could do delete obj.payload.is_exchange
. And to change a value, just use the =
operator. obj.payload.created_at = "2021-03-23"
.