Skip to main content

Combined tree/event based JSON parser

Project description

PyJsonFrag: a powerful combined tree-based and event-based parser for JSON

Typically, JSON is parsed by a tree-based parser unlike XML that can be parsed by a tree-based parser or an event-based parser. Event-based parsers are fast and have a low memory footprint, but a drawback is that it is cumbersome to write the required event handlers. Tree-based parsers make the code easier to write, to understand and to maintain but have a large memory footprint as a drawback. Sometimes, JSON is used for huge files such as database dumps that would be preferably parsed by event-based parsing, or so it would appear at a glance, because a tree-based parser cannot hold the whole parse tree in memory at the same time, if the file is huge.

How to install: PyJsonFrag at PyPI

PyJsonFrag is available at PyPI.

How to install:

python3 -m pip install pyjsonfrag

Example application: customers in a major bank

Let us consider an example application: a listing of a customers in a major bank that has 30 million customers. The test file is in the following format:

{
  "customers": [
    {
      "id": 1,
      "name": "Clark Henson",
      "accountCount": 1,
      "totalBalance": 5085.96
    },
    {
      "id": 2,
      "name": "Elnora Ericson",
      "accountCount": 3,
      "totalBalance": 3910.11
    },
    ...
  ]
}

The example format requires about 100 bytes per customer plus customer name length. If we assume an average customer name is 15 characters long, the required storage is about 115 bytes per customer. For 30 million customers, this is 3.5 gigabytes. In the example, the file is read to the following structure:

class Customer(object):
    def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
        if customerId is not None:
            self.customerId = int(customerId)
        else:
            self.customerId = None
        self.name = name
        if accountCount is not None:
            self.accountCount = int(accountCount)
        else:
            self.accountCount = None
        if totalBalance is not None:
            self.totalBalance = float(totalBalance)
        else:
            self.totalBalance = None

Python jsonstream API

For XML, there is Simple API for XML (SAX). However, for JSON the usual parse methods read the whole data into memory at once, not supporting event-driven parsing. Thus, we provide Python jsonstream API to provide the possibility for event-driven parsing. It is faster and less memory-hungry than the "read all at once" parsing methods, but it is cumbersome.

A jsonstream-based parser is implemented here:

import pyjsonfrag

class Customer(object):
  def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
    if customerId is not None:
      self.customerId = int(customerId)
    else:
      self.customerId = None
    self.name = name
    if accountCount is not None:
      self.accountCount = int(accountCount)
    else:
      self.accountCount = None
    if totalBalance is not None:
      self.totalBalance = float(totalBalance)
    else:
      self.totalBalance = None
  def __repr__(s):
    return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))

context = []
cs = {}
c = None

class MyHandler(pyjsonfrag.JsonHandler):
  def start_dict(stream, key):
    global c
    context.append(key)
    if context == [None, "customers", None]:
      c = Customer()
  def start_array(stream, key):
    context.append(key)
  def end_dict(stream, key):
    context.pop()
  def end_array(stream, key):
    context.pop()
  def handle_string(stream, key, val):
    if key == "name":
      c.name = val
  def handle_number(stream, key, num, is_integer):
    if key == "id":
      cs[int(num)] = c
      c.customerId = int(num)
    elif key == "accountCount":
      c.accountCount = int(num)
    elif key == "totalBalance":
      c.totalBalance = num

handler = MyHandler()
stream = pyjsonfrag.JsonStream(handler)
with open("customers.json", "r") as f:
  while True:
    buf = f.read(4096)
    if buf == '':
      stream.feed(buf, 0, len(buf), True)
      break
    else:
      stream.feed(buf, 0, len(buf), False)
print(cs)

It can be seen that the parser is quite cumbersome and the code to construct a customer is scattered to two different places. Yet it is fast and has a low memory footprint.

Parser with the new library

What if we could combine the benefits of the jsonstream-based approach with the benefits of the "read whole parse tree into memory" based approach? A parse tree fragment for a single customer dictionary is small enough to be kept in memory. This is what the new library is about. Here is the code to parse the customer file with the new library:

import pyjsonfrag

class Customer(object):
  def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
    if customerId is not None:
      self.customerId = int(customerId)
    else:
      self.customerId = None
    self.name = name
    if accountCount is not None:
      self.accountCount = int(accountCount)
    else:
      self.accountCount = None
    if totalBalance is not None:
      self.totalBalance = float(totalBalance)
    else:
      self.totalBalance = None
  def __repr__(s):
    return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))

cs = {}

class MyHandler(pyjsonfrag.FragmentHandler):
  def start_frag_dict(self, key):
    if self.path_is([None, "customers", None]):
      self.start_frag_collection()
  def end_frag_dict(self, key, val):
    if self.path_is([None, "customers", None]):
      c = Customer(customerId=val["id"], name=val["name"],
                   accountCount=val["accountCount"], totalBalance=val["totalBalance"])
      cs[c.customerId] = c

handler = MyHandler()
stream = pyjsonfrag.JsonStream(handler)
with open("customers.json", "r") as f:
  while True:
    buf = f.read(4096)
    if buf == '':
      stream.feed(buf, 0, len(buf), True)
      break
    else:
      stream.feed(buf, 0, len(buf), False)
print(cs)

Note how the code is significantly more simple than for the event-based approach. Performance is close to the event-based approach, and memory consumption is essentially the same as for the event-based approach.

Of course, the new library supports getting the whole parse tree in memory:

import pyjsonfrag

with open("customers.json", "r") as f:
  print(pyjsonfrag.jsonstream_tree_parse(f.read()))

License

All of the material related to PyJsonFrag is licensed under the following MIT license:

Copyright (C) 2026 Juha-Matti Tilli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyjsonfrag-0.2.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjsonfrag-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file pyjsonfrag-0.2.0.tar.gz.

File metadata

  • Download URL: pyjsonfrag-0.2.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for pyjsonfrag-0.2.0.tar.gz
Algorithm Hash digest
SHA256 43960790c609adf14c05b767217084da4b73497f92034f1c00f071dba4bc16bf
MD5 9542d250ad6299bd0ea9c67d1c41da89
BLAKE2b-256 66795cc81d1a414146c30f9b5dc1e4a647543638d81026f441f54827e2b0b7bb

See more details on using hashes here.

File details

Details for the file pyjsonfrag-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyjsonfrag-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for pyjsonfrag-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12c0fae11c51c0e3f20afe52bf2b6f3e10edd12a5c0e9cf3ef4aa356fa03e978
MD5 8e84385ba8a11c5645ebf250bca8ebe5
BLAKE2b-256 23fe944fc59775376d07de384883836eb31844c62d2c55cc7abb03bdd88ed1a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page