Skip to main content

Make Your Json Lovely

Project description

Lovely Json


1. Direct Access to Key Value

**take a look at example to run in code **
This example uses "sample_api.json" so does the following examples"

1-(1). load json

json_data =  open("./sample_api.json", encoding="utf-8", mode='r')

j_sample = Lovely_Json(json.load(json_data), name = 'api_response')	

You need to load json file as you did before in this case too
It was intentionaly designed like this to use at ".json" file and
for json response for servers

In case of using serval jsons, names are recommanded

1-(2). use dot operators to directly get key values

To get data by key

# Lovely_Json :
print(j_sample.Depth4.depth4_item1) # prints "Python"

# Just Json :
original = json.load(open("./sample_api.json", encoding="utf-8", mode='r'))
original["Depth1"]["Depth2"]["Depth3"]["Depth4"]["depth4_item1"] # prints "Python"

To get data, you only need to know the key in the way to make sure what you are finding for. Take a look at image I made To show sample_api.json as graphs

To get Value That are dict, You could directly get it by dot operators (getattr)

print(j_sample.Depth4) # prints {"depth4_item1" : "Python","depth4_item2" : "Haskell"}

But If you want to get Value that isn't dict, You should go through upper key and then get it by dot operators

print(j_sample.Depth4.depth4_item1) # prints "Python"

You could also do by this

print(j_sample.Depth1.depth4_item1) # prints "Python"
print(j_sample.Depth2.depth4_item1) # prints "Python"
print(j_sample.Depth3.depth4_item1) # prints "Python"
print(j_sample.Depth4.depth4_item1) # prints "Python"

2. Access to List of Key Value

# to access item in list, returns list of value of key, "id"
print(j_sample.in_list(key = 'data').id , end='\n\n')
# ---> prints [111, 222, -333]
# and if you have conditions about 'id'
print(j_sample.in_list(key = 'data', if_ ='id', is_ = (lambda x:x>0)).id , end='\n\n') 
# --->prints [111, 222]
# also you could use other key's value of conditions
print(j_sample.in_list(key = 'data', if_ ='type', is_ = (lambda x:x =='articles')).id, end='\n\n')
# --->prints [111, -333]

"if_" and "is_" were desinged to be used like SQL Where syntex, if_ takes key, wher you want to make conditions is_ takes lamda or method that returns Bool ( True, False) to check the value fit in what you required


lovely_json was made to make ease access of json data, when developing systems or services.
There will be continuous update for useful methods and speed improvements,
But using conventional ways will make much more speed improvements.

So Think lovely_json as a tool when you use before releasing, like a rough sketch

Thank You!

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 lovely-json, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size lovely_json-0.0.1-py3-none-any.whl (3.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size lovely_json-0.0.1.tar.gz (2.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page