Skip to content

Any idea how to find easily values of (deeply) nested keys, when the keys not always exist? #31

Answered by kellyjonbrazil
halloleo asked this question in Q&A
Discussion options

You must be logged in to vote

Your solution works, but I agree it might be nicer to do it without a try/except block. One thing you can do when you don't know if a key exists is to use the in operator on the dictionary. It would look something like this:

cat data.json | jello -c '\
res = []
for s in _.details.sections:
    for f in s.fields:
        if "v" in f:
            res.append(f.v)
res'

The output should be:

["Yes!","Indeed!"]

To find the keys no matter how deep they are, you may need to use a recursive method. Fortunately, the -s option in jello happens to be a recursive function, so you can sort of hack a solution using -s with grep:

$ cat data.json | jello -s | grep ".v ="
.details.sections[0].fields[0].v = "

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@halloleo
Comment options

@kellyjonbrazil
Comment options

@halloleo
Comment options

Answer selected by halloleo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #30 on June 25, 2021 04:50.