3

I have a file outputted from an API, it contains a JSON.

{
    "result": {
        "id": "f34fdasdfaa4364adc42b3a57",
        "modified_on": "2018-06-02T17:08:17.106435Z"
    },
    "success": true
}

I need only the value of id (in my case the f34fdasdfaa4364adc42b3a57)

I would do in bash:

#!/usr/bin/env bash

#Pseudocode
id=$(grep -Po '"id":.*?[^\\]",' id.txt)
# /Pseudocode
curl -X DELETE "https://www.example.com/delete/$id"

I did look for previous answer and got this: Parsing JSON with Unix tools

But the right answer extract key and value, I would only value (to no manipulate string anymore)

Thank you for your help

Cyrus
  • 84,225
  • 14
  • 89
  • 153
sineverba
  • 5,059
  • 7
  • 39
  • 84

1 Answers1

10
id=$(jq -r '.result.id' id.txt)

Output to variable id:

f34fdasdfaa4364adc42b3a57
Cyrus
  • 84,225
  • 14
  • 89
  • 153