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
Macos: How to Downgrade Homebrew Python
How to Insert String Value into Specific Column Value on Python Pandas
How to Downgrade Python from 3.7 to 3.5 in Anaconda
How to Clear/Delete the Contents of a Tkinter Text Widget
Pip Error: Microsoft Visual C++ 14.0 Is Required
How to Suppress Scientific Notation When Printing Float Values
Python Xlrd Unsupported Format, or Corrupt File.
Valueerror: Cannot Reshape Array of Size 30470400 into Shape (50,1104,104)
How to Stop Python Script Using Command Line
I Want to Reshape 2D Array into 3D Array
Keras Valueerror: Input 0 Is Incompatible With Layer Conv2D_1: Expected Ndim=4, Found Ndim=5
How to Find a Word That Starts With a Specific Character
How to Import a File in Python With Spaces in the Name
Programme to Print Mulitples of 5 in a Range Specified by User
Move Files Between Two Aws S3 Buckets Using Boto3
How to Extract Hours and Minutes from a Datetime.Datetime Object