55

That is the current json array I have. I want get all json objects that type=1

before filter:

[ 
        {
            "type": 1
            "name" : "name 1",
        }, 
        {
            "type": 2
            "name" : "name 2",
        }, 
        {
            "type": 1
            "name" : "name 3"
        }, 
]

after filter:

[ 
        {
            "type": 1
            "name" : "name 1",
        }, 
        {
            "type": 1
            "name" : "name 3"
        }, 
]

please help.

3
  • On what basis you want to filter your json object ? Commented Nov 28, 2014 at 13:37
  • turn it into the object it represents, delete the item, turn it back into json Commented Nov 28, 2014 at 13:37
  • 10
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. Commented Nov 28, 2014 at 13:38

3 Answers 3

88

The following snippet of code does exactly what you want, but BEWARE that your input (as written in the question) is not a valid json string, you can check here: http://jsonlint.com.

import json

input_json = """
[
    {
        "type": "1",
        "name": "name 1"
    },
    {
        "type": "2",
        "name": "name 2"
    },
    {
        "type": "1",
        "name": "name 3"
    }
]"""

# Transform json input to python objects
input_dict = json.loads(input_json)

# Filter python objects with list comprehensions
output_dict = [x for x in input_dict if x['type'] == '1']

# Transform python object back into json
output_json = json.dumps(output_dict)

# Show json
print output_json
Sign up to request clarification or add additional context in comments.

2 Comments

instead of loading everything and then filtering it, cant you supply a function for object_hook and filter it inside that method?
Is there a way to do this with a nested JSON string?
18

Simply

print [obj for obj in dict if(obj['type'] == 1)] 

Example Link.

Comments

14

The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. Documentation for filter

>>> obj=[
...     {
...         "type": 1,
...         "name": "name 1"
...     },
...     {
...         "type": 2,
...         "name": "name 2"
...     },
...     {
...         "type": 1,
...         "name": "name 3"
...     }
... ]
>>> filter(lambda x: x['type'] == 1, obj)
<filter object at 0x7fd98805ca00>
>>> list(filter(lambda x: x['type'] == 1, obj))
[{'type': 1, 'name': 'name 1'}, {'type': 1, 'name': 'name 3'}]
>>> list(filter(lambda x: x['type'] == 2, obj))
[{'type': 2, 'name': 'name 2'}]

Comments

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.