How to get the value of a key in a JSON? [duplicate] - json

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 3 years ago.
I have a JSON body. I would like to obtain the value of "access_token". I would like to use grep in bash. The grep I used doesn't not work.
I tried the following but it provides me a blank result.
HTTP_BODY=$(echo $HTTP_RESPONSE | grep ^"access_token":" ","experies_in$ )
I want only to obtain the access_token value, i.e.:
eyJhbGciOiJSUzI1NiIsImtpZCI6IkE4M0UwQTFEQTY1MzE0NkZENUQxOTFDMzRDNTQ0RDJDODYyMzMzMzkiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJxRDRLSGFaVEZHX1YwWkhEVEZSTkxJWWpNemsifQ.eyJuYmYiOjE1NTkzMTYzNTYsImV4cCI6MTU1OTMxOTk1NiwiaXNzIjoiaHR0cHM6Ly9jaW5jaHktbnByLmNsb3VkLnJlcy5ibmdmLmxvY2FsL2NpbmNoeXNzbyIsImF1ZCI6WyJodHRwczovL2NpbmNoeS1ucHIuY2xvdWQucmVzLmJuZ2YubG9jYWwvY2luY2h5c3NvL3Jlc291cmNlcyIsImpzX2FwaSJdLCJjbGllbnRfaWQiOiJhcGkiLCJzdWIiOiIxIiwiYXV0aF90aW1lIjoxNTU5MzE2MzU2LCJpZHAiOiJsb2NhbCIsInByb2ZpbGUiOiJBZG1pbmlzdHJhdG9yIiwiZW1haWwiOiJhZG1pbkBjaW5jaHkuY28iLCJyb2xlIjoiQ2luY2h5IFVzZXIgQWNjb3VudCIsImlkIjoiYWRtaW4iLCJzY29wZSI6WyJqc19hcGkiXSwiYW1yIjpbImN1c3RvbSJdfQ.O0--cahxPKlwHp-7fP0CMgSJTaXleupH32x7vVoxe8THVdeRIgyuZoKWPAK9p10PMO9a5Mi3N0t1Nqut5-dUS7lmeUfNKe25K1got9de7ghQ56QQXnL2SWd6g4I8Zi1R9fZsln7bZCIJvnG3_wIWHKGHBco9jEvKtO3AdYF4T9LAbdpT51SDzKPhX16BPc0Do6KfNImQpPQdK4fP3-JqxD4sOBldUg-g3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cYUaLILQ

jq -r '.access_token' file
Output:
eyJhbGciOiJSUzI1NiIsImtpZCI6IkE4M0UwQTFEQTY1MzE0NkZENUQxOTFDMzRDNTQ0RDJDODYyMzMzMzkiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJxRDRLSGFaVEZHX1YwWkhEVEZSTkxJWWpNemsifQ.eyJuYmYiOjE1NTkzMTYzNTYsImV4cCI6MTU1OTMxOTk1NiwiaXNzIjoiaHR0cHM6Ly9jaW5jaHktbnByLmNsb3VkLnJlcy5ibmdmLmxvY2FsL2NpbmNoeXNzbyIsImF1ZCI6WyJodHRwczovL2NpbmNoeS1ucHIuY2xvdWQucmVzLmJuZ2YubG9jYWwvY2luY2h5c3NvL3Jlc291cmNlcyIsImpzX2FwaSJdLCJjbGllbnRfaWQiOiJhcGkiLCJzdWIiOiIxIiwiYXV0aF90aW1lIjoxNTU5MzE2MzU2LCJpZHAiOiJsb2NhbCIsInByb2ZpbGUiOiJBZG1pbmlzdHJhdG9yIiwiZW1haWwiOiJhZG1pbkBjaW5jaHkuY28iLCJyb2xlIjoiQ2luY2h5IFVzZXIgQWNjb3VudCIsImlkIjoiYWRtaW4iLCJzY29wZSI6WyJqc19hcGkiXSwiYW1yIjpbImN1c3RvbSJdfQ.O0--cahxPKlwHp-7fP0CMgSJTaXleupH32x7vVoxe8THVdeRIgyuZoKWPAK9p10PMO9a5Mi3N0t1Nqut5-dUS7lmeUfNKe25K1got9de7ghQ56QQXnL2SWd6g4I8Zi1R9fZsln7bZCIJvnG3_wIWHKGHBco9jEvKtO3AdYF4T9LAbdpT51SDzKPhX16BPc0Do6KfNImQpPQdK4fP3-JqxD4sOBldUg-g3aau2F_DmapEd0p5hTI4qeKgORnXJ3NadwWscQREGWVXhIRu_BF_cmEoIfPNyJI7D_L7EWn8XcFa2Gu-8khQ-WDpVUcpyidF_VHYRkMtYwpJ2dcYUaLILQ
See: man jq

Related

Shell Script jq reading a json key with period (.) inside a variable [duplicate]

This question already has answers here:
Passing bash variable to jq
(10 answers)
"Invalid numeric literal" error from jq trying to modify JSON with variable
(1 answer)
Closed 4 years ago.
Here is the example json
{
"app": "K8s",
"version": "1.8",
"date": "2018-10-10"
}
In order to get the value of app, I can do this in jq as
jq '.app'
But what I want is, I want to pass the key to jq as a bash variable, i.e
bash_var="app"
jq '."${bash_var}"'
I'm getting the output as null instead of the value. What is the correct syntax to achieve this?
First, you need to port the bash variable into jq's context usign the --arg flag and access it inside the [..]
jq --arg keyvar "$bash_var" '.[$keyvar]' json

why get all results of jq in a single array element in bash [duplicate]

This question already has answers here:
How do I assign the output of a command into an array?
(3 answers)
Closed 2 years ago.
I'm trying to get all results of jq query in a array in bash, but it gives me all results in the first array element.
Could you help me? I would like to having every result in one array element
bash-4.2$ try=$(tm run jobs:status::get -s "tm=serverA&work=*&status=Failed" | $PATH_API/jq -r '.statuses[].name // empty')
bash-4.2$ echo $try
job_B job_C
bash-4.2$ echo "${#try[#]}"
1
bash-4.2$
If the status names are sufficiently uncomplicated, you could add an extra pair of parentheses:
try=($(tm run ....))
(Consider also invoking jq without the -r option.)
Otherwise, you could consider using readarray, or other techniques for initializing bash arrays.

JSON object returned to bash need to extract key value [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 4 years ago.
I have a curl command that gets a JSON response and it gives the response below. I need to get the value of data.value
How can I do this without a "hacky" solution?
{
"request_id":"50aaabe7-d01b-0a83-da86-8f01cb1da74b",
"lease_id":"",
"renewable":false,
"lease_duration":2764800,
"data":{"value":"randomBinaryString"},
"wrap_info":null,
"warnings":null,
"auth":null
}
If you install jq, then it is as easy as:
<curl-command> | jq .data.value
If you don't want to install extra software and the response is always in that format, you can do some dirty tricks:
<curl-command> | grep data | tr '":{' ' ' | awk '{print $3}'

How to grep the specific field value from json string using shell script [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 6 years ago.
Below is my JSON string available in file, out of which I need to extract the value for status in shell script.
Expected Output: status=success
response.json
{"eventDate":null,"dateProccessed":null,"fileName":null,"status":"success"}
Please let me know, if you need any information
Look into jq. It is a very handy json parser.
If you want to extract just the status you can do something like:
jq -r '.status' response.json
# output
success
You can format your output as well to follow the results you want.
jq -r '"status=\(.status)"' response.json
# output
status=success
You can try sed:
sed -n 's|.*"status":"\([^"]*\)".*|status=\1|p' response.json

Parse JSON in Bash [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 7 years ago.
I'm trying to get the value of a json array but I don't manage to get the value of a leaf, can anyone help me?
Here's the json from maps.googleapis.com/maps/api
I'd like to get the duration text and value and here's the script I've been using so far.
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
curl --get --include 'http://maps.googleapis.com/maps/api/directions/json?origin=59.94473,30.294254&destination=59.80612,30.308552&sensor=false&departure_time=1346197500&mode=transit&format=json' | jsonValue duration["value"] 2
Thanks in advance,
Jeremie.
You are looking for jq
(a lightweight and flexible command-line JSON processor).
This worked for me
curl -G -k --data "origin=59.94473,30.294254&destination=59.80612,30.308552&sensor=false&departure_time=1346197500&mode=transit" http://maps.googleapis.com/maps/api/directions/json | jq '.routes[].legs[].duration["text"]'