Skip to main content

Help you find the data nested deep in your data

Project description

nested-data-helper

Help you find the data nested deep in your data.

Introduction

I am a data engineer who works with data a lot. Whilst flat data might be straight forward to navigate, it is often not the case for nested data. Especially data from APIs, JSONs, or NoSQL Databases, the schema is not enforced by design and it requires discipline from the developer, which is not always guaranteed. Even if we are lucky to have access to the documentations, these documents might still be outdated or too big to understand.

To efficiently understand and navigate nested data, I have been using the functions in this modules and decided to polish them and publish them for anyone to use. I have used them to understand the schema discipline, find the path to desired data, understand if there are missing data and etc. They may not be helpful in production but they surely are useful being used in REPL or Jupyter notebook. Use cases can be scraping, feature engineering, data mining, data cleansing, edge case finding and on and on. By using these function, you can speed up your data inspection iterations.

This library consist of two parts:

  • navigation: syntactic sugar to help you navigate to the subpath of the complex data with the path syntax
  • pathfinder: help you find the relevant paths you are looking for from paths or from the values

They share the same path structure so you can easily copy the path and see the data for yourself.

This library might not be useful in production as [key] chains work just fine and explore part has limited use in production, but this can speed up your data exploration a lot.

Why nested-data-helper

Nested-data-helper speeds up the process of inspecting nested data such as JSON files, API returned values, or NoSQL Databases, enabling you to achieve quality work with less time. You may also use this tool to inspect the schema discipline of data!

It is lightweight and get the job done! We use core python so installation is a breeze and we can be sure that this will work for years to come even when it seems abandoned.

Installation

You can install the library from PyPi using pip:

pip install nested_data_helper

Navigation

You can get the values of the data without the clumsy chain of square brackets now.

>>> from nested_data_helper.navigation import navigate

>>> data = {
...   "results": [
...      {"name": "happy", "synonyms": [{"text": "happy"}, {"text": "glad"}]},
...      {"name": "sad", "synonyms": [{"text": "sad"}, {"text": "upset"}]},
...   ]
... }
# Instead of this
>>> data["results"][0]["name"]
"happy"

# write this
>>> navigate(data, "results.[0].name")
"happy"
# You can even use a list like this
>>> navigate(data, "results.[].name")  # returns all name under the results list
["happy", "sad"]

# And of course list in lists
navigate(data, "results.[].synonyms.[].text")  # These will be in a flat structure
["happy", "glad", "sad", "upset"]

# And combination of both
navigate(data, "results.[0].synonyms.[].text")
["happy", "glad"]

This is even more powerful combined with the paths returned from explore and will allow quick exploration of your data.

Pathfinder

This helps you understand the data structure you are looking for by filtering out the noise. Combined with Counter, you can find the most common paths in the data, or simply understand if there are missing data. You can also find out the type discipline of the data through the explore functions.

These functions are:

  • find_paths
  • find_paths_unique
  • find_paths_unique_per_doc

The above are useful for understanding the schema of the nested data and data discipline. When combined with Counter, you can quickly understand what paths are the most important in the data set. Other use cases include finding the most common fields that we are not sure the exact path is for like "name", "id". More examples are in the docstring for each function.

  • find_values

This is useful when you know of a value or an aspect of value but are not sure what the paths are. For example, if you are trying to join two data sets with a known key, you could use the key to find the path to the data you need. Or if you want to make sure you have the best path for something, you can use this function to list all the paths that share these values and then evaluate them. More examples are in the docstring for the function.

Development

This library works fine and has past the test (I use doctest for now). This library should have no dependencies other than core python so it is up to you if you want to use a virtual environment.

To run the tests:

python -m unittest tests/**.py

Feel free to send pull requests, issues, or fork this repos to amend as you need.

Known issues

  • The path definition does not distinguish between two levels of keys versus a key with a dot in it. There is no plan to handle edge cases as the limited use cases does not warrant the added complexity, but here are some tricks to deal with it:

When you use navigate, it will return a KeyError if a key with dot exists.

>>> data = {"top_level": {"second.level": "value"}}

>>> navigate(data, "top_level.second.level")
Traceback (most recent call last):
KeyError: 'second'

With that, you know that the second.level key is with a dot. You can use this to find the actual key:

>>> navigate(data, "top_level").keys()
dict_keys(['second.level'])

>>> navigate(data, "top_level")["second.level"]
'value'
  • The path definition does not distinguish keys that is in square brackets and list indices

Similar workaround can be used.

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

nested_data_helper-1.0.1.tar.gz (45.7 kB view details)

Uploaded Source

Built Distribution

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

nested_data_helper-1.0.1-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file nested_data_helper-1.0.1.tar.gz.

File metadata

  • Download URL: nested_data_helper-1.0.1.tar.gz
  • Upload date:
  • Size: 45.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for nested_data_helper-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d13c6540773c4ab92eed83265d11b5430804a8cc0214a3b99f51257f84e6cf3a
MD5 bcdd855d80759377b98b7ee9d70837cb
BLAKE2b-256 9a878abfb478ba53ffbb96f3022aad705735796d46cc5f51489dc04dfddb6f75

See more details on using hashes here.

File details

Details for the file nested_data_helper-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nested_data_helper-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 293805f66185a97f27d352e83ad6a41e1932b726de8611d0b86e255495ecb781
MD5 5f5e571db18dbe44986e0a265254b882
BLAKE2b-256 e116273537b64d39c25f296ef5890a06012437effd15005d70d7b878bd1f925d

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