[Python-ideas] iterator/stream-based JSON API

Antoine Pitrou solipsis at pitrou.net
Mon Jun 3 17:43:04 CEST 2013


On Mon, 3 Jun 2013 09:01:43 -0400
Daniel Holth <dholth at gmail.com> wrote:
> 
> The ijson API yields a stream of events containing the full path to
> each item in the parsed JSON, an event name like "start_map",
> "end_map", "start_array", ...
> 
> list(ijson.parse(StringIO.StringIO("""{ "a": { "b": "c" } }""")))
> 
> [('', 'start_map', None),
>  ('', 'map_key', 'a'),
>  ('a', 'start_map', None),
>  ('a', 'map_key', 'b'),
>  ('a.b', 'string', u'c'),
>  ('a', 'end_map', None),
>  ('', 'end_map', None)]
> 
> It also has a higher-level API yielding only the objects under a
> certain prefix. Pass "a.b" and you would get only "c".
> 
> Besides memory this kind of thing makes it much easier to know which
> level of the JSON structure you are in compared to the existing
> object_pairs hook.

But did you encounter a use case where the existing API didn't fit the
bill?

Regards

Antoine.




More information about the Python-ideas mailing list