Skip to main content

A series of utility functions to help with tidy json via python dictionary.

Project description

jsonMojo

A series of utility functions to help with tidy json via python dictionary.

Installation from github

Use the package manager pip to install jsonMojo.

features

  • replace_element: recursively replace an element by a desired element within a dictionary
  • remove_element: recursively remove a desired element within a dictionary
  • list_flatten: recursively flat a list/array
  • sort_dict: sort a python dictionary by either key or value
  • filter_rowwise: filter a rowwise list/array by selected value(s)
  • sort_rowwise: sort a rowwise list/array by selected key

Usage

import jsonMojo as jm

# test_dict as an example input
test_dict = {
    "A": "1",
    "B": "2",
    "C":{
        "C1": ["1", "45"]
    }
}

Recursively replace an element by a desired element within a dictionary

jm.replace_element(test_dict, to_replace = "1", replace_with = "A1" )
#returns {'A': 'A1', 'B': '2', 'C': {'C1': ['A1', '45']}}

We can set output_json = True if we want the output in JSON string format.

jm.replace_element(test_dict, to_replace = "1", replace_with = "A1" , output_json = True)
#returns '{\n"A": "A1",\n"B": "2",\n"C": {\n"C1": [\n"A1",\n"45"\n]\n}\n}'}

Recursively remove a desired element within a dictionary

jm.remove_element(test_dict, to_remove = "1" )
#returns {'B': '2', 'C': {'C1': ['45']}}
jm.remove_element(test_dict, to_remove = "1" , output_json = True)
#returns '{\n"B": "2",\n"C": {\n"C1": [\n"45"\n]\n}\n}'

recursively flat a list/array

jm.list_flatten(['2', ['1', '3', ['a', '1']]] )
#returns ['2', '1', '3', 'a', '1']

jm.list_flatten(list({"A", "B"}))
#returns ['A', 'B']
jm.list_flatten(['2', ['1', '3', ['a', '1']]] , output_json = True)
#returns '[\n"2",\n"1",\n"3",\n"a",\n"1"\n]'

jm.list_flatten(list({"A", "B"}), output_json = True)
#returns '[\n"A",\n"B"\n]'

sort a python dictionary by either key or value

jm.sort_dict(input_dict = {"A": 2, "BA": 4, "GW": 3, "EW": 1, "W": 0}, sort_by_key = False, descending = False )
# returns {'W': 0, 'EW': 1, 'A': 2, 'GW': 3, 'BA': 4}

jm.sort_dict(input_dict = {"A": 2, "BA": 4, "GW": 3, "EW": 1, "W": 0}, sort_by_key = True, descending = False )
# returns {'A': 2, 'BA': 4, 'EW': 1, 'GW': 3, 'W': 0}

We can set output_json = True if we want the output in JSON string format.

filter a rowwise list/array by selected value(s)

input_rowwise = [
    {"id": 10, "dept": "Biology"},
    {"id": 2, "dept": "Chemistry"},
    {"id": 3, "dept": "Computer Science"}
]
jm.filter_rowwise(input_rowwise, by_key = "dept", filter_in = ["Biology", "Computer Science"])
# returns [{'id': 1, 'dept': 'Biology'}, {'id': 3, 'dept': 'Computer Science'}]

We can set output_json = True if we want the output in JSON string format.

sort a rowwise list/array by selected key

cars = [
  {'car': 'Ford', 'year': 2005},
  {'car': 'Mitsubishi', 'year': 2000},
  {'car': 'BMW', 'year': 2019},
  {'car': 'VW', 'year': 2011}
]
jm.sort_rowwise(cars, by_key = 'car', descending =False)
# returns [{'car': 'BMW', 'year': 2019},
# {'car': 'Ford', 'year': 2005},
# {'car': 'Mitsubishi', 'year': 2000},
# {'car': 'VW', 'year': 2011}]

We can set output_json = True if we want the output in JSON string format.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Project details


Download files

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

Source Distribution

jsonMojo-0.1.1.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file jsonMojo-0.1.1.tar.gz.

File metadata

  • Download URL: jsonMojo-0.1.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.7.3

File hashes

Hashes for jsonMojo-0.1.1.tar.gz
Algorithm Hash digest
SHA256 483d96a8e344a8e9aab0ca0a4927f871464c68aa58bbcfafa94929193e07e339
MD5 139d9c2eb82fbba5f5fe8925c5a2d88c
BLAKE2b-256 18c4fc2299a1c7111cfd3d1e0f903d312a34753454370642900c6a2754dae1a4

See more details on using hashes here.

Supported by

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