ctienshi
ctienshi

Reputation: 133

Extracting values from JSON using bash

{
  "test1": {
    "type": "string",
    "value": "foo"
  },
  "test2": {
    "type": "string",
    "value": "bar"
  }
}

In a json like above I need to get extract the following details. Assuming that I have to extract the key and the value both.

test1=foo

test2=bar

Is there anyway this is possible through bash?

Upvotes: 1

Views: 38

Answers (1)

ctienshi
ctienshi

Reputation: 133

Found the answer. You can do it by the following code.

jq -r 'keys[] as $k | "\($k) \(.[$k].value)"' tmp.json

Upvotes: 1

Related Questions