Make Your Json Lovely
Project description
Lovely Json
Features
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
add
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for lovely_json-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e9276e26a17a88ffdcc572be0f9280bb1eeddb9132ecc24b3d3fa6b1b92f81c |
|
MD5 | 9754887469b9930f5bcc22690f960fa8 |
|
BLAKE2b-256 | 19845c2be0af73b8ed36e06edc79178f46f033d1a9749f1448bc54e4214df000 |