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
Answer from Joaquin Sargiotto on Stack OverflowThe 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
Simply
print [obj for obj in dict if(obj['type'] == 1)]
Example Link.
How to filter Json File in Python?
arrays - Filtering JSON data in python - Stack Overflow
How to filter a JSON?
How to filter json output from file using python? - Stack Overflow
Videos
What you've shown is a single JSON array, not a CSV. (File extensions don't matter to Python)
Parse the names from the objects in each JSON array row
import json
with open("file.txt") as f:
for line in f:
names = (x['name'] for x in json.loads(line))
for name in names:
print(name)
Something like this should work. You use the csv library to convert the rows into an array to iterate over. The ast library will turn a string into a dict/json object you can key into.
import ast
import csv
names = []
with open('csvFile.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
names.append(ast.literal_eval(row)[name])
# or you could print(name) here.
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
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']
Let's say we can use dict comprehension, then it will be
output_dict = [{k:v for k,v in x.items() if k in ["Item", "Price"]} for x in input_dict]
You can also do it like this :)
>>> [{key: d[key] for key in ['Item', 'Price']} for d in input_dict] # you should rename it to `input_list` rather than `input_dict` :)
[{'Item': 10, 'Price': 8.9}, {'Item': 15, 'Price': 2.6}]
Ideally I would like to write a function which would take a varying number of key-value pairs as a dynamic filtering criteria.
I'm pretty much a python newbie still and would really like to learn. I should be in the right place 😄.