Skip to main content

Parse invalid, malformed and barely JSON-esque data

Project description

Barely JSON

GitHub Actions badge

A Python package for parsing data that only looks like JSON

from barely_json import parse
print(parse('[what is this?, , {perhaps, json: no}]'))

# Prints ['what is this?', '', {'perhaps': '', 'json': False}]

Quite a bit of data looks like JSON at a first glance but turns out not to comply completely with the JSON specification -- often because the exporting software is broken, but sometimes simply because the format was never intended to be JSON in the first place.

No matter how you ended up with the data, now you want to parse it! However, most JSON parsers are pretty strict, so you're out of luck with your JSON-esque mess.

That's where Barely JSON steps in and tries to parse anything that remotely looks like JSON. In addition to the pure parsing, Barely JSON will also try to post-process your data into suitable Python types even if your data provider uses, for example, on and off as boolean literals.

Installation

The supported Python versions are 3.7 and later.

pip install barely_json

Usage

The main routine is parse:

> from barely_json import parse
> parse("[NaN, , {state: off, where's my value?}, NULL]")

[nan, '', {'state': False, "where's my value?": ''}, None]

As you can see, parse by default tries to convert values that are illegal in JSON into hopefully appropriate Python types, which often works well. But sometimes that's not what you want, so you can disable the auto-conversion:

> parse("[NaN, , {state: off, where's my value?}, NULL]", resolver=None)

[<IllegalValue 'NaN'>,
 <IllegalValue ''>,
 {<IllegalValue 'state'>: <IllegalValue 'off'>,
  <IllegalValue "where's my value?">: <IllegalValue ''>},
 <IllegalValue 'NULL'>]

In that case any value that's illegal or missing is wrapped in an instance of a special IllegalValue class. You can also provide your own resolver for illegal values, which is simply a callback that maps strings to arbitrary values:

> from barely_json import default_resolver
>
> def my_resolver(text):
>     if text.lower() == 'one':
>         return 1
>     return default_resolver(text)
>
> parse('[one, FALSE]', resolver=my_resolver)

[1, False]

When writing your own resolver it's often handy to fall back todefault_resolver after you've handled your special cases.

Change Log

See CHANGELOG.md.

License

Distributed under the MIT license. See the file LICENSE for details.

Contributors

Development

Clone the repository:

git clone https://github.com/torfsen/barely_json.git
cd barely_json

Install the development dependencies

pip install -r requirements-dev.txt

Run the tests:

tox

For pull requests, the tests are run using GitHub actions.

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

barely_json-1.0.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

barely_json-1.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file barely_json-1.0.0.tar.gz.

File metadata

  • Download URL: barely_json-1.0.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for barely_json-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1bbde93cc4cd5fe7992ac24de3fd43b843717f7460700927c3233ae4b1ccee42
MD5 4e8fc7583a8c4d91119bf69db272749e
BLAKE2b-256 03bbf7c274654c8b23cd8e8d107779515b670d44244b6b983b5dff32070b6af7

See more details on using hashes here.

File details

Details for the file barely_json-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: barely_json-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for barely_json-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31511356a359a70bcfe741a26fe05dc1efacca30d145721618b13e5b933e3819
MD5 8de2aa3f1eca99407501c92908f68b08
BLAKE2b-256 d2cbeefd729ec41bd0662b68710fac2f4312199847d626461b079007d368f0cc

See more details on using hashes here.

Supported by

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