${json_obj}= Load JSON From File C:\\temp\\example.json
JSONLibrary.Delete Object From Json ${json_obj} $..address
This is how to delete address.
But if I want to add address with some value, how to do it?
Tried this but it does not add:
${object_to_add}= Create Dictionary latitude=13.1234 longitude=130.1234
${json_obj}= Add Object To Json ${json_obj} $..address ${object_to_add}
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"gender": "male",
"favoriteColor": [
"blue"
],
"isMarried": false,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [{
"type": "iPhone",
"number": "0123-4567-8888"
}, {
"type": "home",
"number": "0123-4567-8910"
}]
}
It could be that after trying to delete "address", the json path "$..address" no longer exists and you won't be able to add objects to it.
You could check for the existence of the "address" key in the main object and if not exists then create it with the coordinates as the keys value
*** Settings ***
Library JSONLibrary
Library Collections
*** Test Cases ***
Scenario: Add Objects
${current_record} Load JSON From File C:\\temp\\example.json
Delete Object From Json ${current_record} $..address
${coords} Create Dictionary latitude=13.1234 longitude=130.1234
# Check if the "address" key is in the object
${is_address_key} Run Keyword And Return Status Dictionary Should Contain Key ${current_record} address
# If address key not exists then add coords to dictionary with key address and add to object directly
IF ${is_address_key}
Add Object To Json ${current_record} $..address ${coords}
ELSE
${address} Create Dictionary address=${coords}
Add Object To Json ${current_record} $ ${address}
END
Dictionary Should Contain Key ${current_record}[address] latitude
Log To Console \n${current_record}