JSON data recognized as a string field instead of integer

老子叫甜甜 提交于 2019-12-13 21:33:41

问题


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)

回答1:


It appears json.dump alone is most likely treating everything as strings. Using json.loads after it should convert it into a list or dict allowing you to use the json values as integers.

Example:

x = json.dumps({'1': 2, '3': 4}, ensure_ascii=True, sort_keys=True)
y = json.loads(x)
print(y)
print(y['3']+1)

Output:

{u'1': 2, u'3': 4}
5

You could convert the string value to int without using json.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 the type your json.dump result is try doing:

x = json.dump(json_data,f,ensure_ascii=True)
print(type(x))

https://docs.python.org/3.3/library/json.html



来源:https://stackoverflow.com/questions/29482512/json-data-recognized-as-a-string-field-instead-of-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!