Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ijson parse file (ijson from softwaremaniacs.org )

Tags:

python

json

unix

I need a little help to parse a large JSON file. Here I have just a sample of the data (only 2 items).

I need to use the parse method. open() does not work, because the file is too large.

parser=ijson.parse("sample.json")

I need to loop and print out the Identifier from all the Assets.

It cannot be so hard, but I cannot get the correct code.

Thank you for any helpful tips.

Peter

json data:

{
  "AssetCount": 2,
  "Server": "xy",
  "Assets": [
    {
      "Identifier": "21979c09fc4e6574"
    },
    {
      "Identifier": "e6235cce58ec8b9c"
    }
 ]
}
like image 330
user3022366 Avatar asked Jan 22 '26 03:01

user3022366


1 Answers

Try ijson.parse(open('sample.json')). The output will look something like

list(ijson.parse(open('sample.json')))

[('', u'start_map', None),
 ('', u'map_key', u'Server'),
 (u'Server', u'string', u'xy'),
 ('', u'map_key', u'Assets'),
 (u'Assets', u'start_array', None),
 (u'Assets.item', u'start_map', None),
 (u'Assets.item', u'map_key', u'Identifier'),
 (u'Assets.item.Identifier', u'string', u'21979c09fc4e6574'),
 (u'Assets.item', u'end_map', None),
 (u'Assets.item', u'start_map', None),
 (u'Assets.item', u'map_key', u'Identifier'),
 (u'Assets.item.Identifier', u'string', u'e6235cce58ec8b9c'),
 (u'Assets.item', u'end_map', None),
 (u'Assets', u'end_array', None),
 ('', u'map_key', u'AssetCount'),
 (u'AssetCount', u'number', 2),
 ('', u'end_map', None)]

ijson is also available from pypi.

like image 67
joeforker Avatar answered Jan 23 '26 16:01

joeforker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!