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.
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
Asked: 2022-06-20 11:00:00 +0000
Seen: 8 times
Last updated: Jun 17 '21
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?