Skip to main content

utilities for working with newline delimited JSON (NDJSON) files

Project description

Utilities for working with newline delimited JSON (NDJSON) files, aka JSON Lines.

Latest release 20250528: New dump_ndjson and dumps_ndjson for transcribing obj as NDJSON (includes the trailing newline).

Short summary:

  • append_ndjson: Append an iterable of objects to a file as newline delimited JSON.
  • dump_ndjson: Write obj as a single line of JSON, including the trailing newline.
  • dumps_ndjson: Return obj as a single line of JSON, including the trailing newline.
  • scan_ndjson: Read a newline delimited JSON file, yield instances of dictclass (default dict, otherwise a class which can be instantiated by dictclass(a_dict)).
  • UUIDNDJSONMapping: A subclass of IndexedSetMixin which maintains records from a newline delimited JSON file.
  • write_ndjson: Transcribe an iterable of objects to a file as newline delimited JSON.

Module contents:

  • append_ndjson(f, objs): Append an iterable of objects to a file as newline delimited JSON.

  • dump_ndjson(obj, f): Write obj as a single line of JSON, including the trailing newline.

  • dumps_ndjson(obj): Return obj as a single line of JSON, including the trailing newline.

  • scan_ndjson(f, dictclass=<class 'dict'>, error_list=None): Read a newline delimited JSON file, yield instances of dictclass (default dict, otherwise a class which can be instantiated by dictclass(a_dict)).

    error_list is an optional list to accrue (lineno,exception) tuples for errors encountered during the scan.

  • Class UUIDNDJSONMapping(cs.obj.SingletonMixin, cs.mappings.IndexedSetMixin)``: A subclass of IndexedSetMixin which maintains records from a newline delimited JSON file.

UUIDNDJSONMapping.__init__(self, filename, dictclass=<class 'cs.mappings.UUIDedDict'>, create=False): Initialise the mapping.

Parameters:

  • filename: the file containing the newline delimited JSON data; this need not yet exist
  • dictclass: a optional dict subclass to hold each record, default UUIDedDict
  • create: if true, ensure the file exists by transiently opening it for append if it is missing; default False

UUIDNDJSONMapping.add_backend(self, record): Append record to the backing file.

UUIDNDJSONMapping.rewrite_backend(self): Rewrite the backing file.

Because the record updates are normally written in append mode, a rewrite will be required every so often.

UUIDNDJSONMapping.scan(self): Scan the backing file, yield records.

  • write_ndjson(f, objs): Transcribe an iterable of objects to a file as newline delimited JSON.

Release Log

Release 20250528: New dump_ndjson and dumps_ndjson for transcribing obj as NDJSON (includes the trailing newline).

Release 20211208: Initial PyPI release.

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

cs_ndjson-20250528.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

cs_ndjson-20250528-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cs_ndjson-20250528.tar.gz.

File metadata

  • Download URL: cs_ndjson-20250528.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cs_ndjson-20250528.tar.gz
Algorithm Hash digest
SHA256 351683cab8634c5ffa0b09bc90e2ce405f363d7f9589ac2d17a34782fead007a
MD5 2d86a22a718f55edd652bcaac927dcca
BLAKE2b-256 19265d2ef26fcff1cd195a969c4bcac1639cba42c827ea91ed52b939ec2d3226

See more details on using hashes here.

File details

Details for the file cs_ndjson-20250528-py2.py3-none-any.whl.

File metadata

  • Download URL: cs_ndjson-20250528-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cs_ndjson-20250528-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ab46e55c156b1d89509794212511c8527bd9a2cb8f47d6f8fd26181c8908e01a
MD5 e253cf8325914f05e701c481293b06d6
BLAKE2b-256 56ed4df507cb6a53d1d38a0d520fae7ac1f6be058ba9310bc613d9ebc8a78555

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