How to Find and Replace a Part of a Value in Json File

How to find and replace value in JSON?

Do something like this. Convert to string replace using regex (add key to regex as well) and then convert back.


var data = {
"responses": {
"firstKey": {
"items": {
"name": "test name one"
}
},
"anotherKey": {
"items": {
"name": "test name two"
}
},
"oneMoreKey": {
"items": {
"name": "John"
}
}
}
};

var originalMsg = JSON.stringify(data);
console.log(data)
console.log(originalMsg)
var updatedMsg = originalMsg.replace(/test name [a-z]*/g, "N/A");
console.log(updatedMsg)
var newObj = JSON.parse(updatedMsg);
console.log(newObj);

How to find and replace a value in a JSON file?

import json
data = json.load(open('mydata.json'))
data["825734065351577641"]["prefix"] = '/'
json.dump(data, open('mydata.json','w')

python replace value in json file [closed]

You can do something like this.

import json 
with open('/path/to/josn_file.json', 'r') as file:
json_data = json.load(file)
for item in json_data:
if item['ParameterKey'] in ["Shell","Type"]:
item['ParameterKey'] = "new value"
with open('/path/to/josn_file.json', 'w') as file:
json.dump(json_data, file, indent=2)

Bash script - unable to replace string with double quotes and curly braces

Using sed

$ output_bucket=AAA
$ new_bucket_name="{\"Fn::Sub\": \"$output_bucket-\${AWS::Region}\" }"
$ cdk_bucket_name=hnb659fds-assets-xxccddff
$ sed s"/\"$cdk_bucket_name\"/$new_bucket_name/" input_file
{ "AWS679f53fac002430cb0da5b7982bd22872D164C4C": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {"Fn::Sub": "AAA-${AWS::Region}" },
"S3Key": "68b4ffa1c39cb3733535725f85311791c09eab53b7ab8efa5152e68f8abdb005.zip"
},
"Role": {
"Fn::GetAtt": [
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "nodejs12.x",
"Timeout": 120
},
"DependsOn": [
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2"
],
"Metadata": {
"aws:cdk:path": "CODE/AWS679f53fac002430cb0da5b7982bd2287/Resource",
"aws:asset:path": "asset.68b4ffa1c39cb3733535725f85311791c09eab53b7ab8efa5152e68f8abdb005",
"aws:asset:is-bundled": false,
"aws:asset:property": "Code"
}
}
}

Replace JSON value with corresponding value from another JSON file in Python

We take the dictA data field(It is a list) to iterate. For each element in the list We take the value of the element since the element is a dictionary and check whether it is in the dataB 'data' field list elements then we change the dataA['street'] field

dictA ={'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}
dictB = {'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}


for elementA in dictA['data']:
for elementB in dictB['data']:
if elementA['street'] in elementB:
elementA['street'] = elementB[elementA['street']]
print(dictA)

Using zip function, Iterate two list in same time

dictA ={'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}
dictB = {'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}

for elementA,elementB in zip(dictA['data'], dictB['data']):
elementA['street'] =elementB[elementA['street']]
print(dictA)



Related Topics



Leave a reply



Submit