Skip to main content

A saner way to parse JSON.

Project description

https://travis-ci.org/skorokithakis/jsane.svg?branch=master

JSane is a JSON “parser” that makes attribute accesses easier.

Three-line intro

>>> import jsane
>>> j = jsane.loads('{"foo": {"bar": {"baz": ["well", "hello", "there"]}}}')
>>> j.foo.bar.baz[1].r()
u'hello'

Motivation

Picture the scene. You’re a jet-setting developer who is obsessed with going to the gym. One day, a world-class jewel thief kidnaps you and asks you to hack into the super-secure bank server in thirty seconds, while an ultramodel is performing oral sex on you. You hurriedly trace the protocol on the wire, only to discover, to your dismay, that it uses JSON. Nested JSON, with levels and levels of keys.

It’s hopeless! You’ll never type all those brackets and quotation marks in time! Suddenly, a flash of a memory races through your mind, like some cliche from a badly-written README. You launch the shell and type two words:

import jsane

The day is saved.

Motivation (non-Hollywood version)

Are you frustrated with having to traverse your nested JSON key by key?

root = my_json.get("root")
if root is None:
    return None

key1 = root.get("key1")
if key1 is None:
    return None

key2 = key1.get("key2")
if key2 is None:
    return None

<five more times>

Is your code ruined by pesky all-catching except blocks?

try:
    my_json["root"]["key1"]["key2"]["key3"]
except:
    return None

Are you tired of typing all the braces and quotes all the time?

my_json["root"]["key1"[""]][]"]']'"}}""]

Now there’s JSane!

Motivation (non-infomercial version)

Okay seriously, this["thing"]["is"]["no"]["fun"]. JSane lets you traverse.json.like.this.r(). That’s it.

Usage

Using JSane is simple, at least. It’s pretty much a copy of the builtin json module.

First of all, install it with pip or easy_install:

pip install jsane

Here’s an example of its usage:

>>> import jsane

>>> j = jsane.loads('{"some": {"json": [1, 2, 3]}}')
>>> j.some.json[2].r()
3
You can also load an existing dictionary::
>>> j = jsane.from_dict({"hi": "there"})
>>> j.hi
'there'

If the dictionary contains any data types that aren’t valid in JSON (like functions), it still should work, but you’re on your own.

Due to Python being a sensible language, there’s a limit to the amount of crap you can pull with it, so JSane actually returns a Traversable object on accesses:

>>> j = jsane.loads('{"foo": {"bar": {"baz": "yes!"}}}')
>>> type(j.foo)
Traversable

If you want your real object back at the end of the wild attribute ride, call .r():

>>> j.foo.bar.r()
{"baz": "yes!"}

If an attribute, item or index along the way does not exist, you’ll get an exception. You can get rid of that by specifying a default:

>>> import jsane

>>> j = jsane.loads('{"some": "json"}')
>>> j.haha_sucka_this_doesnt_exist.r(default="💩")
"💩"

“But how do I access a key called r?!”, I hear you ask. Worry not, I got you covered:

>>> j.key["r"].more_key.r()

Confused? Don’t name your keys r, then.

That’s about it. I’m not loving the r() API, so if anyone has any good recommendations on how I may better fulfil my unholy purpose, I’m changing it on the spot. No guarantees of stability before version 1, as always. Semver giveth, and semver taketh away.

Help needed/welcome/etc, mostly with designing the API. Also, if you find this library useless, let me know.

License

BSD. Or MIT. Whatever’s in the LICENSE file. I forget. It’s permissive, though, so relax.

Self-promotion

It’s me, Stavros.

FAQ

  • Do you find it ironic that the README for JSane is insane?

    No.

  • Is this library awesome?

    Yes.

  • I hate the .r() thing, is there any way to avoid it?

    Did you even read this README?

    Alright, there is now a way to avoid it. Instead of j.foo.bar.r(), you can just call the last key, i.e. j,foo.bar(). Let me know what you think in the relevant issue.

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

jsane-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distributions

jsane-0.1.1-py3.6.egg (6.7 kB view details)

Uploaded Source

jsane-0.1.1-py2-none-any.whl (7.5 kB view details)

Uploaded Python 2

File details

Details for the file jsane-0.1.1.tar.gz.

File metadata

  • Download URL: jsane-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsane-0.1.1.tar.gz
Algorithm Hash digest
SHA256 21dd7908b9f9797ce4baf2d79df43a298b2346be3788897684e7749107016ab8
MD5 db11df8e49a50cc5150fcaef8bc81392
BLAKE2b-256 4c6472a16831504454ca345f80614c45ef4d3837d796baa2abab37b8cb282b7c

See more details on using hashes here.

File details

Details for the file jsane-0.1.1-py3.6.egg.

File metadata

  • Download URL: jsane-0.1.1-py3.6.egg
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsane-0.1.1-py3.6.egg
Algorithm Hash digest
SHA256 ef89fa9d83f711ea780666b26a023e2c79e4cf3ce312c2cd2a6554d406fc7eb2
MD5 6ec11eac96033d45945c64c24974a62d
BLAKE2b-256 6bed0f4fd81008a8e28600e9cd61191a059db3af0ed7c3a02fb5177878f72b29

See more details on using hashes here.

File details

Details for the file jsane-0.1.1-py2-none-any.whl.

File metadata

File hashes

Hashes for jsane-0.1.1-py2-none-any.whl
Algorithm Hash digest
SHA256 eca33311c09f6e223909b2ac055775f87d9cc803a348d0a4c63ece6bf53f8aa3
MD5 7387d4a43e97b3adfc799847c6b1c6c6
BLAKE2b-256 36772ee2a25b61aec567cf5e71d584343992f2b27bc5401e36539ea810c13fd2

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