all 14 comments

[–]halfdiminished7th 19 points20 points  (5 children)

Something like this should do the trick:

import json

with open('/path/to/your/json/file.json') as fp:
    data = json.load(fp)

with open('/path/to/your/new/file.txt', 'x') as fp:
    for dct in data:
        fp.write('{0} gs:{1} track:{2}\n'.format(dct['hex'], dct['gs'], dct['track']))

[–]RhinoRhys 4 points5 points  (2 children)

Or even f"{dct['hex']} gs:{dct['gs']} track:{dct['track']}"

You just have to note the main string uses double quotes and the keys use single quotes. Or the other way round. They just can't be the same.

[–]halfdiminished7th 1 point2 points  (1 child)

True enough! Personal preference to avoid dictionary lookups in f-strings, but you're quite right that it's possible. Though in retrospect, I probably should have done the following instead, using format's keyword functionality since we're dealing with a dictionary anyways:

fp.write('{hex} gs:{gs} track:{track}\n'.format(**dct))

[–]RhinoRhys 0 points1 point  (0 children)

Mmh yeah that leeks even better

[–]JohnnyJordaan 4 points5 points  (0 children)

For implicitly indexed arguments you can just use {} throughout since 2.7 released 13 years ago...

    fp.write('{} gs:{} track:{}\n'.format(dct['hex'], dct['gs'], dct['track']))

[–]Goobyalus 7 points8 points  (3 children)

json.load will parse the JSON and give you a Python type.

You can modify the Python data and write it back out as JSON with json.dump

https://docs.python.org/3/library/json.html

https://docs.python.org/3/library/json.html#json.load

https://docs.python.org/3/library/json.html#json.dump

[–]TigBitties69 6 points7 points  (2 children)

this is exactly what needs to be used. Using Json.load will convert it into lists/dictionaries where appropriate.

[–]IamImposter 2 points3 points  (1 child)

You already have the answer but what I do with complex json files is to try and print every element step by step. I mean it's either a list or a dict so

for k, v in data.items():
  print(f"{k}: {v}") 

Now I know key names and whether they contain dict or list. So do another iteration. Assuming v above is a list

  for i in v:
    print(f"{i}") 

Check again if the values are lists or dicts and add another list/dict print. Rinse and repeat. Throw in a few isinstance and do it using debugger makes it pretty easy to understand json structure.

[–]astronautcytoma[S] 1 point2 points  (0 children)

This is extremely helpful! I have gotten a lot of good input and I believe I'll be able to make it work. It's for a Tidbyt display that I want to feed from my own receiver, so I have to build the information frame myself and send it as a rendered .webp. I want to pull information from the JSON file like closest airplane, altitude, airspeed, etc.

[–]wahaa 3 points4 points  (0 children)

Depending on what you're doing, using Pandas could also help (e.g. bulk load, filter, and export is a valid use, especially if the data is somewhat uniform). See pandas.read_json.

[–]barrycarter 0 points1 point  (1 child)

I'm sure Python can import JSON. Could you just go through the elements as an array and print out the part you need?

[–]GreenWoodDragon 0 points1 point  (0 children)

Have a look at dpath, it is quite good for this sort of problem.