Skip to content

This package will help you to save your time while parsing json or dictionary in python by just inheriting the class. For more info read README.md.

Notifications You must be signed in to change notification settings

kumar-sanchay/python-jsonparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-jsonparser

This package will help you to reduce your stress :) while parsing json in python. Follow the steps below for using python-jsonparser

  1. Install the package from pypi
pip install python-jsonparser
  1. Now let's assume we have a json object as below.
dummy = {
    "name": {
        "first_name": "ABC",
        "last_name": "EFG"
    },
    "details": {
        "phone":["xxxxxxxxx", "00xxxxxxx", "0000000000"]
    }
}
  1. Now assume we want first_name, last_name and only first phone number from the list. For this create a class TestClass as below and inherit JSONParser from :
from json_parser import JSONParser

class TestClass(JSONParser):
    first_name = 'name/first_name'
    last_name = 'name/last_name'
    phone = 'details/$1'
  1. Create instance of your class and pass the json object.
test_obj = TestClass(dummy)
  1. Now call the validate method from the object created.
output = test_obj.validate()
  1. You will have following as the output:
{
    'first_name': 'ABC',
    'last_name': 'EFG',
    'phone': '00xxxxxxx'
}
  1. For getting the range of elements from the list you can use $x:$y
from json_parser import JSONParser

class TestClass(JSONParser):
    first_name = 'name/first_name'
    last_name = 'name/last_name'
    phone = 'details/$1:$3' # This will give you 2 values from the list

# Output:
# {
#     'first_name': 'ABC',
#     'last_name': 'EFG',
#     'phone': ['00xxxxxxx', '0000000000']
# }
  1. This was all about single object. What if you have json list of objects. In that case set many=True while creating the instance of your class. For example:
test_obj = TestClass(dummy, many=True)

About

This package will help you to save your time while parsing json or dictionary in python by just inheriting the class. For more info read README.md.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages