1

We'd like to filter the following json output from testers.txt for any userid's in list form:

{
    "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.

3
  • you sure about the input being a valid json? Commented Feb 10, 2017 at 10:10
  • No, what shall I change? Commented Feb 10, 2017 at 10:12
  • Next time when you are unsure about a json being a valid json, using this website to validate it Commented Feb 10, 2017 at 10:19

3 Answers 3

3

You need a leading and trailing square bracket on your json file, like so:

[{
    "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
Sign up to request clarification or add additional context in comments.

Comments

0

You will know the json is an invalid one upon using this website to validate the json.

You need to add a [] to make the json in a list of jsons as you json is not a valid one.

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']

Comments

-1

use jq can achieve what you want easily.

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

"16214222"
"44223333"

assume you json in below format:

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

1 Comment

The question asks how to do it using python and your answer uses jq instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.