Skip to main content

Json shorthand for python

Project description

Jshort

Python shorthand for json. inspired by q

This package allows json load, dump from and to file with few code.

Installation

pip install jshort

Usage

# This is an instance
import j 

# Loads json from file, stores it in data property
h = j(input_path='/tmp/data.json')

# Get data
h.data
#{
#   "foo": "bar"
#}

# Display colored content
print(h)
# or
h.prt()
# {
#     "oh": "dayum!"
# }

# The same, in short
# For data
j(i='/tmp/data.json').d
# Print
j(i='/tmp/data.json').prt()

# Write json
j(output_path='/tmp/out.json', data={"foo": "bar"})

# Also write data in a shorter way and change print content setting indentation to 2
j(o='/tmp/out.json', d={"hey": "jude"}, indent=2).prt()
# {
#   "hey": "jude"
# }

# Traversing json documents using https://pypi.org/project/jsonpath-ng/
j(d=my_dict).path('key')

# Shorter and with more filtering
j(d=my_dict).p('sub.*')

# Or fintering on an array and from a file
j(i='/tmp/data.json').p('key[1]')

Have a look at https://pypi.org/project/jsonpath-ng/ for more about json traversing.

If only one result is returned from filtering, the path method returns only the result otherwise it is a list of results.

Json as object

It is possible to manipulate json in j instance as a python object. Try the following:

o = j(d={'a': {'b': 1}}).obj

#raw data
o.data_
#{'a': {'b': 1}}

#pretty print it
print(o)
#dumps with syntax coloration the json content

o.c
#None

o.a
#{'b': 1}

o.a.b
#1

o.a['test'] = 1
print(o.d_)
#{'a': {'b': 1}, 'test': 1}

Enjoy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for jshort, version 1.1.2
Filename, size File type Python version Upload date Hashes
Filename, size jshort-1.1.2-py2.py3-none-any.whl (3.8 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size jshort-1.1.2.tar.gz (3.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page