Home > other >  loop through each line in a json file and filter the result
loop through each line in a json file and filter the result

Time:12-01

Hi I have a json file that contains something as below. I want to write a code to look through each line in the file and filter out the items that is of "emoji": "1" and output the result to another json file.

Input

{"o": [{"item": {"emoji": "0"}}]}
{"p": [{"item": {"emoji": "1"}}]}
{"q": [{"item": {"emoji": "1"}}]}
{"r": [{"item": {"emoji": "0"}}]}

Output

{"p": [{"item": {"emoji": "1"}}]}
{"q": [{"item": {"emoji": "1"}}]}

CodePudding user response:

The easiest way to work with the structure of your json data, would be to iterate over all lines in the file and check if a specific string is in the line:

filtered_json = []
with open('data.json', 'r') as file:
    for line in file:
        if '"emoji": "1"' in line:
            filtered_json.append(line)

this is of course a bit sloppy but will be a quick fix.

you can now write the filtered_json with:

with open('new_data.json', 'w') as file:
    new_data = '\n'.join(filtered_json)
    file.write(new_data)

The more complicated way of working with your json data would be using the json package:

import json
filtered_json = []
with open('data', 'r') as file:
    for line in file:
        j = json.loads(line)
        if int(j[list(j.keys())[0]][0]['item']['emoji']) == 1:
             filtered_json.append(j)

this is pretty bad and writing data to a file is going to be a weird as well.


we can make this a lot better by altering your json data, into actual json format:

old data.json:

{"q": [{"item": {"emoji": "1"}}]}
{"r": [{"item": {"emoji": "0"}}]}

new data.json:

{
    "r": {"item": {"emoji": 0}},
    "q": {"item": {"emoji": 1}}
}

We can now use this new data.json file in a simpler way by working with the json package:

import json
with open('data.json', 'r') as file:
    j = json.load(file)
    filtered_json = {}
    for key in j.keys():
        if j[key]['item']['emoji'] == 1:
            filtered_json[key] = j[key]

By altering the json file we achieved a much simpler code. We can even use a dict comprehension like this:

with open('data.json', 'r') as file:
    j = json.load(file)
    filtered_data = {k: v for k, v in j.items() if v['item']['emoji'] == 1}

which is probably the most pythonic way of doing this


You can write the filtered_json here by just calling:

with open('new_data.json', 'w') as file:
    json.dump(filtered_json, file)
  • Related