Skip to content
Advertisement

Python: change json data according to provided paths (e.g. jsonpath)

I need a tool to modify json data with some arbitrary function applied only to specific paths expressed in some syntax (e.g. jsonpath, but may be any other). Key feature is to modify existing data structure, not to extract only some piece and then modify it. Is there something like I described?

For example we got the input data:

JavaScript

and I want to drop users’ last names. So, I need to apply the function like lambda x: x.split(' ')[0] to the jsonpath like $.users..name

as a result I want to get

JavaScript

This is a very simple example, input data and path expression may be more sophisticated

Advertisement

Answer

I’ve found the solution using jsonpath-ng.

JavaScript

As a result input_data will contain an original dictionary but with updated values finded by path

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement