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:
- I have a static json file (add.json)
- I'm running two OS shell commands within python, and storing that output into separate text files.
- I then want to take the values in those two txt files, and replace the two strings in a json file.
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.