3

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:

  1. I have a static json file (add.json)
  2. I'm running two OS shell commands within python, and storing that output into separate text files.
  3. 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.

1
  • You might take a look at Python's json encoder which converts JSON-data into dictionaries. After that you could adapt this dictionary and write it back to a JSON-file. Commented Aug 31, 2015 at 13:05

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

You have a typo, fout/four.
Does changing the variable change["Name"] also changes the variable json_data?
2

just open that file as normal text file and replace the string you want to

with open('file.json', 'r+') as file:
    content = file.read()
    file.seek(0)
    content.replace('string_replaced', 'new_string')
    file.write(content)

Since you want to replace the string everywhere, it doesn't matter whether data is json or not

2 Comments

Even that your solution will work, it's not a good practice to work with json that way. (just sayin)
Thank you for your time to respond. the 'new_string' is supposed to be replaced with the contents I have in echo.txt, and when I put that, I get: file.write(content) IOError: [Errno 0] Error. Below is what I currently have: import os import fileinput import json cmd = 'hostname > host.txt' cmd2 = 'echo mama > echo.txt' os.system(cmd) os.system(cmd2) file2 = open('echo.txt') contents2 = file2.read() with open('add.json', 'r+') as file: content = file.read() content.replace('dns', contents2) file.write(content)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.