Skip to content

Searching and Removing nested element in JSON payload without explicit check #284

Discussion options

You must be logged in to vote

Thanks, I did it.

Here's the code.

def deleteByKey(jsonObj, searchTerm)
    if (is-object($jsonObj))
      {for ($jsonObj)
        .key:
        if(.key != $searchTerm) 
           if (is-object(.value) or is-array(.value))
              deleteByKey(.value, $searchTerm)
           else
              .value
      }
    else if (is-array($jsonObj))
       [for($jsonObj)
            deleteByKey(., $searchTerm)
       ]
 
deleteByKey(., "shipping_address")

Do you think I can optimize it or is there any test case issues? Do let me know.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@larsga
Comment options

Answer selected by akashvekariya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
support Users asking how to solve a specific issue
2 participants