I'm new to python and what I'm trying to do is replace text/string in a json file, from a python os shell command. I'm somewhat getting the results I'm looking for, but it's appending extra whitespace/creating a new line in the json file. This is basically what I'm trying to accomplish:
Below is what I currently have (to make it simple I've replaced the true aws cli commands with simple commands)
import os
import fileinput
cmd = 'hostname > host.txt'
cmd2 = 'echo mama > echo.txt'
os.system(cmd)
os.system(cmd2)
file = open('host.txt')
contents = file.read()
with open("out.json", "wt") as fout:
with open("add.json", "rt") as fin:
for line in fin:
fout.write(line.replace('dns',contents))
file2 = open('echo.txt')
contents2 = file2.read()
with open("out2.json", "wt") as fout2:
with open("out.json", "rt") as fin2:
for line in fin2:
fout2.write(line.replace('ip', contents2))
And this is the result that it's yielding:
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "WildburritoPC
",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "mama
"
}
]
}
}
]
}
As you can see, after Name and Value, it indeed replaces the values, but adds a new line and produces invalid json.
This is the file I'm replacing the values in:
{
"Comment": "A new record set for the zone.",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "dns",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "ip"
}
]
}
}
]
}
Thank you in advance for any answers. I know what I have above is very dirty, and I'm sure there must be a better/cleaner way of accomplishing what I'm trying to do, but ultimately I know we all have to start somewhere and I can't even begin to explain how grateful I am with this community for all the help it's provided so far.
There's json module in python's standard library, it'll be much more error proof to use it rather than replacing strings.
To load json file:
import json
with open("add.json", "r") as fout2:
json_data = json.load(fout2)
for change in json_data["Changes"]:
# strip the contents of trailing white spaces (new line)
change["Name"] = change["Name"].strip()
# dump json to another file
with open("out.json", "w") as fout:
fout.write(json.dumps(json_data))
I guess you got the idea. json module will take care that your json data are not corrupted (or at least it'll fail with exception when that occurs).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With