简体   繁体   English

如何使用python过滤文件中的json输出?

[英]How to filter json output from file using python?

We'd like to filter the following json output from testers.txt for any userid 's in list form:我们想过滤来自testers.txt的以下 json 输出,以获取列表形式的任何userid

{
    "status": true,
    "user": {
        "user_id": "16214222",
        "username": "tester11"
    }
},
{
    "status": true,
    "user": {
        "user_id": "44223333",
        "username": "tester22"
    }
}

What we currently have (tried old code):我们目前拥有的(尝试过旧代码):

import json

with open('testers.txt') as fp:
     inText = fp.read()
data = json.loads(inText)
print data['user_id']

Finally the output should be:最后输出应该是:

16214222,
44223333

We currently get the following error:我们目前收到以下错误:

Traceback (most recent call last):
  File "start.py", line 5, in <module>
    data = json.loads(inText)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 7 column 2 - line 14 column 2 (char 105 - 212)

I'm new at stackoverflow - feel free to comment my question so I can improve myself.我是 stackoverflow 的新手 - 请随时评论我的问题,以便我提高自己。

You need a leading and trailing square bracket on your json file, like so:您的json文件需要一个前导和尾随方括号,如下所示:

[{
    "status": true,
    "user": {
        "user_id": "16214222",
        "username": "tester11"
    }
},
{
    "status": true,
    "user": {
        "user_id": "44223333",
        "username": "tester22"
    }
}]

And then you can do the following:然后您可以执行以下操作:

import json

with open('testers.txt') as fp:
    data = json.load(fp)

for user in data:
    print user['user']['user_id']

returning:返回:

16214222
44223333

You will know the json is an invalid one upon using this website to validate the json.使用本网站验证json,您将知道该json 是无效的。

You need to add a [] to make the json in a list of jsons as you json is not a valid one.您需要添加一个 [] 以使 json 成为 json 列表中的 json,因为您的 json 不是有效的。

inText = '''
[{
    "status": true,
    "user": {
        "user_id": "16214222",
        "username": "tester11"
    }
},
{
    "status": true,
    "user": {
        "user_id": "44223333",
        "username": "tester22"
    }
}]
'''

import json

with open('testers.txt') as fp:
    inText = fp.read()
data = json.loads(inText)
print [d['user']['user_id'] for d in data]

Output:输出:

[u'16214222', u'44223333']

use jq can achieve what you want easily.使用 jq 可以轻松实现你想要的。

# jq '.[] | .user | .user_id' testers.txt

"16214222"
"44223333"

assume you json in below format:假设你的 json 格式如下:

[{
    "status": true,
    "user": {
        "user_id": "16214222",
        "username": "tester11"
    }
},
{
    "status": true,
    "user": {
        "user_id": "44223333",
        "username": "tester22"
    }
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM