title: Append the data to existing json file
body: <p>I have an existing JSON file and trying to add string into file. But as soon as I write a JSON file the new line characters do disappear in the JSON file and format gets changed.</p>
  
  <p>Below is the code:</p>
  
  <pre><code>#!/usr/bin/python
  import json
  
  userinput = raw_input('Enter the name of a file a you want to read: ')
  with open(userinput) as json_data:
      s = json_data.read()
      data = json.loads(s)
      print data['classes']
  json_data.close()
  
  
  class_add = raw_input('Enter the name of a class a you want to add: ')
  if class_add in data['classes']:
      print "Class %s already exists, doing nothing." % class_add
  else:
      data['classes'].append(class_add)
      print json.dumps(data)
      print data['classes']
      with open(userinput, 'w') as json_data:
          json_data.write(json.dumps(data))
      json_data.close()
  </code></pre>
  
  <hr>
  
  <p>One more import thing here is, that the formatting of the JSON file. So by default we will be having the file in the below formatting.</p>
  
  <pre><code># cat test.json
  {
      "selinux_mode": "enforcing",
      "cis_manages_auditd_service": true,
      "classes": [ "basic", "admin", "lvm"]
  }
  #
  </code></pre>
  
  <p>But once we add the class it becomes the following.</p>
  
  <pre><code># cat test.json 
  {"cis_manages_auditd_service": true, "classes": [ "basic", "admin", "lvm"], "selinux_mode": "enforcing"}
  </code></pre>
  
  <p>Is there any way that I can keep the JSON whitespace and new line character as it is without changing anything.</p>