Skip to main content

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.

Release files for barely-json 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for barely-json 1.0.0
File Size Uploaded
barely_json-1.0.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for barely-json 1.0.0
File Interpreter ABI Platform
barely_json-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / barely_json-1.0.0.tar.gz

Download URL barely_json-1.0.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1bbde93cc4cd5fe7992ac24de3fd43b843717f7460700927c3233ae4b1ccee42
BLAKE2b-256 checksum
How to use checksums
03bbf7c274654c8b23cd8e8d107779515b670d44244b6b983b5dff32070b6af7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.12

Release files / barely_json-1.0.0-py3-none-any.whl

Download URL barely_json-1.0.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31511356a359a70bcfe741a26fe05dc1efacca30d145721618b13e5b933e3819
BLAKE2b-256 checksum
How to use checksums
d2cbeefd729ec41bd0662b68710fac2f4312199847d626461b079007d368f0cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.12

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page