Skip to main content

A saner way to parse JSON.

Project description

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

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 Github 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. That’s it.

Usage

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

>>> import jsane

>>> j = jsane.loads('{"some": "json"}')
>>> j.some
"json"

If the key 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(default="💩").haha_sucka_this_doesnt_exist
"💩"

Due to Python being a non-insane language, there’s a limit to the amount of crap you can pull with it, so JSane actually returns a Traversable object if you access a dict or list:

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

If you want your object back, call .resolve():

>>> j.foo.resolve()
{"bar": "baz"}

That’s about it. I’m not loving this API, so if anyone has any good recommendations on how I may better fulfil my unholy purpose, I’m changing the API 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.

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.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distributions

jsane-0.0.1-py2.7.egg (6.2 kB view details)

Uploaded Source

jsane-0.0.1-py2-none-any.whl (6.8 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for jsane-0.0.1.tar.gz
Algorithm Hash digest
SHA256 bc83e4ee784e1dc63c6f80298fe113f45b1daba7b66c47c0aae5e9150a2e31de
MD5 0c9f1d29938abee6e48c479a9983840d
BLAKE2b-256 ded0c4012c3e478afdf24f23b90ce46785ee7630cfd6d85086d49dc6ea894c1c

See more details on using hashes here.

File details

Details for the file jsane-0.0.1-py2.7.egg.

File metadata

  • Download URL: jsane-0.0.1-py2.7.egg
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsane-0.0.1-py2.7.egg
Algorithm Hash digest
SHA256 65a2acac07ed3f953d5d0a6c54314ea68f539314e0a28b9d7bd836173cb9862c
MD5 d8b6167bcf4f7ddfc6f33d834b11e207
BLAKE2b-256 9a2d8c17d6a4bf5dfc3bfade8f1b660602a4d4bc6d5ac1561fe17937d2c347ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsane-0.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 84804448fd934593c957e8a73a118c61b845fbb947fb6b6f7a8f3afa68479121
MD5 dc0d65fa2a7de71dd4a73ed07f53e204
BLAKE2b-256 96eb13e5e9d9aaa230287c7fdace8a5b46a07b0f9071df86e0b829fd8b5ed704

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