butterfingers
butterfingers

Reputation: 11

How do I parse data from a JSON file and save it to another text file in shell script

Suppose I have this JSON file,

{
   "tags": [
    {
      "name": "xxx1",
      "image_id": "yyy1"
    },


    {
      "name": "xxx2",
      "image_id": "yyy2"
    }
  ]
}

I want to parse just the values from key 'name' and save it to another text file example.txt.

My desired output in the text file would be

xxx1

xxx2

How do I do this?

Upvotes: 1

Views: 1607

Answers (2)

ufopilot
ufopilot

Reputation: 3975

jq -r '.tags[].name' input.json > output.txt

Upvotes: 1

lazyBug
lazyBug

Reputation: 99

Save your data in temp.json and use the below command.

cat temp.json | grep name | cut -d ":" -f 2 | cut -d "," -f 1 >> output.txt

Upvotes: 0

Related Questions