All Projects → torfsen → Barely_json

torfsen / Barely_json

Licence: mit
A Python parser for data that only looks like JSON

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Barely json

Stream Parser
⚡ PHP7 / Laravel Multi-format Streaming Parser
Stars: ✭ 391 (+598.21%)
Mutual labels:  json, parser
Crossplane
Quick and reliable way to convert NGINX configurations into JSON and back.
Stars: ✭ 407 (+626.79%)
Mutual labels:  json, parser
Json Rust
JSON implementation in Rust
Stars: ✭ 395 (+605.36%)
Mutual labels:  json, parser
Bad json parsers
Exposing problems in json parsers of several programming languages.
Stars: ✭ 351 (+526.79%)
Mutual labels:  json, parser
Jkt
Simple helper to parse JSON based on independent schema
Stars: ✭ 22 (-60.71%)
Mutual labels:  json, parser
Csv Parser
A modern C++ library for reading, writing, and analyzing CSV (and similar) files.
Stars: ✭ 359 (+541.07%)
Mutual labels:  json, parser
Jsonparser
One of the fastest alternative JSON parser for Go that does not require schema
Stars: ✭ 4,323 (+7619.64%)
Mutual labels:  json, parser
Pxi
🧚 pxi (pixie) is a small, fast, and magical command-line data processor similar to jq, mlr, and awk.
Stars: ✭ 248 (+342.86%)
Mutual labels:  json, parser
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+1255.36%)
Mutual labels:  json, parser
Himalaya
JavaScript HTML to JSON Parser
Stars: ✭ 758 (+1253.57%)
Mutual labels:  json, parser
Ojg
Optimized JSON for Go
Stars: ✭ 281 (+401.79%)
Mutual labels:  json, parser
Parson
Lightweight JSON library written in C.
Stars: ✭ 965 (+1623.21%)
Mutual labels:  json, parser
C Sharp Stack Only Json Parser
Stack only json deserialization using generators and the System.Text.Json library
Stars: ✭ 254 (+353.57%)
Mutual labels:  json, parser
Choetl
ETL Framework for .NET / c# (Parser / Writer for CSV, Flat, Xml, JSON, Key-Value, Parquet, Yaml, Avro formatted files)
Stars: ✭ 372 (+564.29%)
Mutual labels:  json, parser
Rdflib Jsonld
JSON-LD parser and serializer plugins for RDFLib (Python 2.6+)
Stars: ✭ 250 (+346.43%)
Mutual labels:  json, parser
Tomlplusplus
Header-only TOML config file parser and serializer for C++17 (and later!).
Stars: ✭ 403 (+619.64%)
Mutual labels:  json, parser
Sdk
Library for using Grafana' structures in Go programs and client for Grafana REST API.
Stars: ✭ 193 (+244.64%)
Mutual labels:  json, parser
Baby
Create models from a JSON file, even a Baby can do it.
Stars: ✭ 214 (+282.14%)
Mutual labels:  json, parser
Body Parser
Node.js body parsing middleware
Stars: ✭ 4,962 (+8760.71%)
Mutual labels:  json, parser
Xml Js
Converter utility between XML text and Javascript object / JSON text.
Stars: ✭ 874 (+1460.71%)
Mutual labels:  json, parser

Barely JSON

Travis CI 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 2.7 as well as 3.3 and later.

As usual it's recommended to use a virtualenv to keep things tidy.

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.

Development

Clone the repository:

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

Install tox locally in a virtualenv if you haven't installed it globally:

virtualenv venv
source venv/bin/activate
pip install tox

Run the tests:

tox
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].