Home > Back-end >  Filter JSON Array in python
Filter JSON Array in python

Time:07-08

Its me again. I have a big one line JSON Array which I get as a response from a url.

It looks something like this:

[{"completed":"XXXXXXXXX","flow":"XXXXXX","process":"XXXXX","step":"XXXXXXX","thingname":"INEEDTHISFORLATER"},{"completed":"XXXXXXXXXX","flow":"XXXXXXXX","process":"XXXXXXX","step":"XXXXXXXX","thingname":"INEEDTHISFORLATER"}]

And I need to filter the lines like so that I only extract "INEEDTHISFORLATER" and save it to a new file.

I tried many things with pandas but it didnt work because its a one-liner aparrently.

Thanks for your help!

CodePudding user response:

You can use the json packge to handle your case

import json

s = '[{"completed":"XXXXXXXXX","flow":"XXXXXX","process":"XXXXX","step":"XXXXXXX","thingname":"INEEDTHISFORLATER"},{"completed":"XXXXXXXXXX","flow":"XXXXXXXX","process":"XXXXXXX","step":"XXXXXXXX","thingname":"INEEDTHISFORLATER"}]'

obj = json.loads(s)

with open('file.txt', "w") as file:
    for item in obj:
        file.write(item['thingname']   "\n")

file.txt:

INEEDTHISFORLATER
INEEDTHISFORLATER
  • Related