0

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!

3
  • 1
    I strongly suggest you use the Python program to read the incoming JSON as JSON, and then in iterate over the JSON outputting one line at a time. Commented Jul 7, 2022 at 16:54
  • Does it have to be interpreted as JSON? I dont have that much knowledge with that so I thought I can maybe prevent using JSON interpretations and just treat it as a .txt file. Commented Jul 7, 2022 at 16:56
  • 1
    There is a package called json in native python. see this Commented Jul 7, 2022 at 17:00

1 Answer 1

1

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

10 Comments

Thanks for the nice explanation. Might use that but I just reworked my question if that makes the solution easier! If not, I will use your solution. Thank you!!
Updated according to your new question.
Perfect! Can I just define s as "file = open('TEMPFILE.json', "r")" ?
Updated again, I missed the output part in my answer
Thank you! If I let "s" open my tempfile.json which is a long one line json array I get this error: "The JSON object must be str not TextIOWrapper"
|

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.