title: Python - remove spaces between key and values from dictionary object
body: <p>I write python script that read JSON from file, transform it and at the end write modified JSON to file. JSON that I read doesn't have white characters, because I need minimal size. </p>
  
  <p>The problem is when I read JSON from file and call <code>json.loads(json_content)</code> I get <code>dictionary</code> object and python adds spaces between key and values. </p>
  
  <p>JSON in file:</p>
  
  <p><code>{"metadata":{"info":"important info"},"timestamp":"2018-04-06T12:19:38.611Z","content":{"id":"1","name":"name test","objects":[{"id":"1","url":"http://example.com","properties":[{"id":"1","value":"1"}]}]}}
  </code></p>
  
  <p>My script:</p>
  
  <pre><code>def get_contents_from_json(file_path):
      with open(file_path) as file:
          contents = file.read()
          print(contents)
          print(type(contents))
          print(sys.getsizeof(contents))
          print(len(contents))
          return json.loads(contents)
  
  
  if __name__ == '__main__':
      data = get_contents_from_json(STACK_JSON)
      print(data)
      print(type(data))
      print(sys.getsizeof(data))
  
      string_data = json.dumps(data)
      print(string_data)
      print(type(string_data))
      print(sys.getsizeof(string_data))
      print(len(string_data))
  
      trimmed = string_data.replace(" ", "")
      print(contents)
      print(type(trimmed))
      print(sys.getsizeof(trimmed))
      print(len(trimmed))
  </code></pre>
  
  <p>And the output of this piece of code is:</p>
  
  <pre><code>{"metadata":{"info":"important info"},"timestamp":"2018-04-06T12:19:38.611Z","content":{"id":"1","name":"name test","objects":[{"id":"1","url":"http://example.com","properties":[{"id":"1","value":"1"}]}]}}
  
  &lt;class 'str'&gt;
  255
  206
  {'metadata': {'info': 'important info'}, 'timestamp': '2018-04-06T12:19:38.611Z', 'content': {'id': '1', 'name': 'name test', 'objects': [{'id': '1', 'url': 'http://example.com', 'properties': [{'id': '1', 'value': '1'}]}]}}
  &lt;class 'dict'&gt;
  240
  {"metadata": {"info": "important info"}, "timestamp": "2018-04-06T12:19:38.611Z", "content": {"id": "1", "name": "name test", "objects": [{"id": "1", "url": "http://example.com", "properties": [{"id": "1", "value": "1"}]}]}}
  &lt;class 'str'&gt;
  273
  224
  {"metadata":{"info":"importantinfo"},"timestamp":"2018-04-06T12:19:38.611Z","content":{"id":"1","name":"nametest","objects":[{"id":"1","url":"http://example.com","properties":[{"id":"1","value":"1"}]}]}}
  &lt;class 'str'&gt;
  252
  203
  </code></pre>
  
  <p>Explanation:</p>
  
  <p>Python reads JSON from file as string that is the same in the file. Length of <code>contents</code> is 206 and size is 255 because empty string in python has 49 bytes. </p>
  
  <p>Calling <code>json.loads(contents)</code> returns <code>dict</code> object that has size 240. JSON represented as <code>dict</code> object has added spaces between key names and values. And this is the cause why after call <code>string_data = json.dumps(data)</code> size of <code>string_data</code> is equal to 274 and length is equal to 224. Therefore I tried to remove white spaces from <code>string_data</code> using function <code>.replace(" ", "")</code>, but I forget that some values have white spaces and after this operation JSON is not correct.</p>
  
  <p>I need to read JSON as dict object to convenient transformations and at the same time I have to keep minimal size of JSON.</p>
  
  <p>Is there any way to remove unnecessary white spaces from <code>dict</code> object?</p>