Reputation: 899
Tried to update the array in json object. Here is my json object
{
"api.version": "v1",
"source": {
"thirdPartyRef": {
"resources": [{
"serviceType": "AwsElbBucket",
"path": {
"pathExpression": "songs/*"
},
"authentication": {
"type": "S3BucketAuthentication"
}
}]
}
}
}
Code that reads json and update awsId. My requirement is to add aws creds int the authentication secition.
Once program run successfully, it should look like
"authentication": {
"type": "S3BucketAuthentication",
"awsId": "AKIAXXXXX",
"awsKey": "MYHSHSYjusXXX"
}
Here is my snippet of code args[5] is the jsonfile
with open(args[5]) as json_data:
source = json.loads(json_data.read())
# source['source']['category']['awsID'] = "test"
source.update( {"awsId" : "AKIAXXXXX", "awsKey": "HHSJSHS"})
print source
output:
{u'api.version': u'v1', 'awsKey': 'HHSJSHS', 'awsId': 'AKIAXXXXX', u'source': {u'thirdPartyRef': {u'resources': [{u'path': {u'pathExpression': u'songs/*'}, u'serviceType': u'AwsElbBucket', u'authentication': {u'type': u'S3BucketAuthentication'}}]}}}
I tried to source.update( "source":{"awsId" : "AKIAXXXXX", "awsKey": "HHSJSHS"}})
, it overwrites the rest of the json.
Upvotes: 0
Views: 68
Reputation: 168626
The data structure that you want to update is buried fairly deeply. You can't access it from the very top level.
Try this:
import json
with open('arg5.json') as json_data:
source = json.loads(json_data.read())
print source
source["source"]["thirdPartyRef"]["resources"][0]["authentication"].update(
{"awsId" : "AKIAXXXXX", "awsKey": "HHSJSHS"})
Upvotes: 4