Skip to main content

Javascript Object Notation (JON) to access JSON and YAML data.

Project description

Javascript Object Notation

Allows Access to JSON/YAML data using Javascript Object Notation.

Note: This code can be applied to any Python dictionary, not only JSON or YAML.

Installation

pip install javascript-object-notation

Access to JSON values

Lets image we have following JSON object.

{  
    "employee": {  
        "name":       "Mario",   
        "salary":      1000000
    }  
}  

The python way to access data inside JSON is using brackets and quotes like a normal dictionary.

import json
json_obj1 = json.loads(json_txt)
assert json_obj1['employee']['name'] == 'Mario'
assert json_obj1['employee']['salary'] == 1_000_000

Using the JSON wrapper you can access data like a normal object.

from jon.jon_wrapper import JONFactory
...
json_obj2 = JONFactory.wrap(json_obj1)
assert json_obj2.employee.name == 'Mario'
assert json_obj2.employee.salary == 1_000_000

Changing values

Is also possible modify values and get json updated.

json_obj2.employee.name = 'Cesar'
json_obj2.employee.salary = 500_000

expected = '{"employee": {"name": "Cesar", "salary": 500000, "married": true, "occupation": "writer"}}'
 assert f'{json_obj2}' == expected

Special symbols

Some JSON properties have special characters like '$', those are not accepted as property names. This library remove automatically those from the names and put it back when you serialize to JSON.

For instance for the below example.

    {  
        "type": "array",
        "$id": "https://spec.openapis.org/oas/3.1/schema/2022-10-07",
        "then": {
          "$ref": "#/$defs/reference"
        }
    }  

You will be able to access like this:

json_obj1 = json.loads(json_txt)
json_obj2 = JONFactory.wrap(json_obj1)

assert json_obj2.type == 'array'
assert json_obj2.id == 'https://spec.openapis.org/oas/3.1/schema/2022-10-07'
assert json_obj2.then.ref == '#/$defs/reference'

expected = '{"type": "array", "$id": "https://spec.openapis.org/oas/3.1/schema/2022-10-07", "then": {"$ref": "#/$defs/reference"}}'
assert f'{json_obj2}' == expected

Access to YAML values

Lets image we have following YAML object.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: consumer
    version: v1
  name: consumer
spec:
...
  template:
    ...
    spec:
      containers:
        - image: emmerson/cdi-rabbit-consumer:1.1.0
          imagePullPolicy: IfNotPresent
          name: cdi-rabbit-consumer
...

Using the JSON wrapper you can access data like a normal object.

d = yaml.safe_load(file)
json_obj2 = JONFactory.wrap(d)

assert json_obj2.kind == 'Deployment'
assert json_obj2.metadata.labels.app == 'consumer'
assert json_obj2.spec.template.spec.containers[0].image == 'emmerson/cdi-rabbit-consumer:1.1.0'

If yaml file contains multiple manifests, you can iterate them and wrap.

docs = yaml.safe_load_all(stream)
all = [JONFactory.wrap(doc) for doc in docs]
assert len(all) == 10

Serialization

By default this library serialize its content as JSON string.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

javascript_object_notation-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file javascript_object_notation-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for javascript_object_notation-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c89c8ccea7cff234c8868f72e96f8bec4aa1d03dd5778807b08b875d8f8ae7ac
MD5 0bfe78afcd5aecb0e80a8a1f23551564
BLAKE2b-256 9ea9644758b06fef96e3eb23d7e0a27cdf6ade95cfced3c83e7ac5c34291f529

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page