I have a small python program that I use to manipulate JSON files. I find that when I dump the manipulated data back to the JSON file using json.dump() it changes to <type 'NoneType'>
. The original type of the data below (type of json_data
) is <type 'dict'>
. I am storing these JSON documents in elasticsearch and visualising it using Kibana4.Kibana4 is treating newly added integer fields as string. Has someone come across this issue before.
import json
fname = "json_data.txt"
with open(fname, 'r+') as f:
json_data = json.load(f)
print(type(json_data))
#Code to add fields to json files.
f.seek(0)
x = json.dump(json_data,f,ensure_ascii=True)
print(type(x))
json.dump()
has no return value. It writes to the file, not return the dumped object.
As such the default return value for a callable, None
is returned instead.
In other words, your code is doing exactly what you asked it to do: read JSON data and parse it, the result is stored in json_data
. You then write that Python object back into the file. The return value of json.dump()
is irrelevant here, you still have a reference to json_data
.
If you wanted a string value containing the JSON object, use json.dumps()
(note the s
); this returns the resulting JSON string without writing to a file:
fname = "json_data.txt"
with open(fname, 'r') as f:
json_data = json.load(f)
print(type(json_data))
json_string = json.dumps(json_data)
print(type(json_string))