Ask Your Question
4

What is the method to retrieve a specific json element using python on the Linux command line without utilizing jq?

asked 2022-06-20 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-17 19:00:00 +0000

ladyg gravatar image

One method to retrieve a specific json element using python on the Linux command line without utilizing jq is to use the following command:

python -c "import json,sys;obj=json.load(sys.stdin);print(obj['key'])" < file.json

Replace 'key' with the specific key of the element you want to retrieve from file.json. The command reads the json data from file.json via standard input (stdin) and prints the value of the specified key.

You can also use the grep command to extract the value of a specific key as follows:

grep -Po '"key": *\K.*?(?=,|\})' file.json

Replace 'key' with the specific key of the element you want to retrieve from file.json. The -Po option of grep extracts only the matched pattern, and the Regex pattern \K.*?(?=,|\}) extracts the value of the specified key by looking for the key followed by a colon (ignores spaces), and then matching any characters until a comma or closing curly brace is encountered.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-20 11:00:00 +0000

Seen: 8 times

Last updated: Jun 17 '21