Skip to main content

json-ql

PyPI version Upload Python Package

Getting Started

  • Prerequisites

    • Python >= 3.6.x
    • pip
  • Installation

    $ pip install json-ql
    
  • Examples

    • Using the JSON ql
    # import the class
    from json_ql import JSONQL
    
    test_json = Fetch().json()
    keys = ['data.median.firstView.loadTime',]
    # returns a new dict with specified keys
    JSONQL(test_json).pick(keys=keys).exec()
    
    
    • Using the JSONQL like a normal dictionary
    # create an instance of the JSONQLDict
    # this creates a read only copy of original dictionary
    test_json = JSONQLDict({"name": "John Doe", "details": {"age": 23}})
    
    # query using the sample query language
    test_json['details.age']
    > 23
    
  • Key Types:

    • key_name

      • Works like simple JSON extraction. Provide the name of the key and boom!.
      • For extraction from beyond first level, append keys with a separator and provide key_delimiter for the program to recognize the start of next level.
      • Example:
      """
      {
          keylevel11: {
              keylevel21: value,
              keylevel22: {
                  keylevel31: value
              }
          }
      }
      """
      # key to extract first level
      # keylevel11
      # returns {keylevel21: ...}
      
      # key to extract second level, first key
      # keylevel11.keylevel21
      # returns value
      
    • [list_index]

      • When you have a list as value and you want to extract a specific index value/object.
      • For extraction from beyond first level, append keys with a separator and provide key_delimiter for the program to recognize the start of next level.
      • Example:
      """
      {
          keylevel11: {
              keylevel21: [
                  1, 2, 3
              ],
              keylevel22: {
                  keylevel31: value
              }
          }
      }
      """
      # key to extract second level, third index
      # keylevel11.keylevel21.[2]
      # returns 3
      
    • [{key=value}]

      • When you have a unordered list of object and you want extract a specific object from the list based on the key and value inside the object
      • For extraction from beyond first level, append keys with a separator and provide key_delimiter for the program to recognize the start of next level.
      • Example:
      """
      {
          keylevel11: {
              keylevel21: [
                  1, 2, 3
              ],
              keylevel22: {
                  keylevel31: value
              },
              keylevel23: [
                  {
                      name: Awesome,
                  },
                  {
                      name: Package
                  }
              ]
          }
      }
      """
      # key to extract second level, and from that extarct the object with name = Awesome
      # keylevel11.keylevel23.[{name=Awesome}]
      # return {name: Awesome}
      
    • [{key~regex}]

      • When you have a unordered list of object and you want extract a specific object from the list based on the key and a regex of value inside the object
      • For extraction from beyond first level, append keys with a separator and provide key_delimiter for the program to recognize the start of next level.
      • The regex search is case sensitive. You do not need to add // or r'' to write your regex.
      • The search will find all the matches and return a list
      • Example:
      """
      {
          keylevel11: {
              keylevel21: [
                  1, 2, 3
              ],
              keylevel22: {
                  keylevel31: value
              },
              keylevel23: [
                  {
                      name: Awesome123,
                  },
                  {
                      name: Package
                  }
              ]
          }
      }
      """
      # key to extract second level, and from that extarct the object with name matching Awesome
      # keylevel11.keylevel23.[{name~Awesome}]
      # return [{name: Awesome}]
      
    • [[ ... ]]

      • When the dict has a nested list and search needs to be done inside those wrap with [] to loop throught the list and find the data.
      • Inside the bracket [] add other expressions to do the lookup at inner most nested array.
      • Add [] pair for each nesting.
      • [] in this is separate from [index], [{key~regex}], [{key=value}]. If there is a nested list of dictionary and we want to find an object by matching key value. Expression will look like this [[{key=value}]]
        • The outer most [] is for the outer list and inner [] is part of the key lookup expression syntax.
      • Example:
      """
      {
          keylevel11: {
              keylevel21: [
                  [{"key_arr": "1"}, {"key_arr": "4"}], 
                  [{"key_arr": "2"}, {"key_arr": "5"}], 
                  [{"key_arr": "3"}, {"key_arr": "6"}]
              ],
          }
      }
      """
      # key to extract second level, and from that extarct the object with name matching Awesome
      # keylevel11.keylevel21.[[{key_arr=5}]]
      # return {"keylevel11.keylevel21.[[{key_arr=5}]]": {"key_arr": "5"}}
      

Available Methods

  • JSONQL
Method Params type default Description
pick key str None selects a key to be returned
pick keys list [] selects a list key to be returned
pick key_delimiter str "." Separator used to identify multi level JSON
pick key_mapping dict {} Mapping of keys for picking with custom key name.

Note:

  • The package is under development and will be prone to more frequent updates

Release files for json-ql 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for json-ql 0.2.2
File Interpreter ABI Platform
json_ql-0.2.2-py3-none-any.whl Python 3 none any Details

Release files / json_ql-0.2.2-py3-none-any.whl

Download URL json_ql-0.2.2-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c3d5095e1110da87b05c7e3611e2f3b0df42cc9b9ba2681ecd60ff59464341c
BLAKE2b-256 checksum
How to use checksums
92e39703e14ed6a603f5fe0cee659d51fc41e1fd8f3f8fc4649affef17465ed6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.15

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page