Skip to content
Advertisement

get list of json paths in python

I’m looking to get list of all possible json paths in a json file – can recommend any one?

Eg : if input is below

JavaScript

o/p :

JavaScript

I’m basically looking. a python version of this : https://www.convertjson.com/json-path-list.htm

I want to build a general solution , if any json file – it will be a single value for schema generation (ie one line in a newline delimeted json) Any suggestions ?

Advertisement

Answer

You can do this in a reasonably succinct way with a recursive generator. The string "obj" is a little awkward since it doesn’t occur in the data structure. On the other hand, adding it at the end is simple:

JavaScript

Gives you paths as a list of strings:

JavaScript

Of course, you can wrap that last step in a function like and accept a root object string:

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