I use the below python code to manipulate a set of JSON files in a specified folder. I extract nt
from the data and I want to create new key value pair. If I were to print nt
on my screen I get values as shown below.
nt 223
nt 286
nt 315
These looks like integers to me. However If I use Kibana visualization tool to process it says this (i.e NXT
is an analysed string fields). I want these values to be recognized as integers? Does this have something to do with the way I am encoding my json file (ensure_ascii=True
) or is JSON value
always a string?
#Process 'new' events to extract more info from 'Messages'
rootDir = '/home/s_parts'
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
fname='s_parts/'+fname
with open(fname, 'r+') as f:
json_data = json.load(f)
m = json_data['Msg']
nt = int(re.findall(r"NXT:\s*([^,m)]*)",m)[0])
json_data["NXT"] = nt
f.seek(0)
json.dump(json_data,f,ensure_ascii=True)
It appears
json.dump
alone is most likely treating everything as strings. Usingjson.loads
after it should convert it into a list or dict allowing you to use the json values as integers.Example:
Output:
You could convert the
string
value toint
without usingjson.loads
, however, converting it to a list or dict with json.loads is far easier to manage and retrieve values. If you are curious about thetype
yourjson.dump
result is try doing:https://docs.python.org/3.3/library/json.html