Home > Back-end >  parse json and get values with same type
parse json and get values with same type

Time:01-10

In the below json, I'm unable to get the value which have reporter only. the output should be jhoncena only which should written into a file.

jq -r '.values' response.json | grep reporter

the output for this is

"name": "reporter-jhoncena"
{
  "size": 3,
  "limit": 25,
  "isLastPage": true,
  "values": [
    {
      "name": "hello-world"
    },
    {
      "name": "test-frame"
    },
    {
      "name": "reporter-jhoncena"
    }
  ],
  "start": 0
}

CodePudding user response:

jq -r '.values[]
  | select(.name|index("reporter"))
  | .name
  | sub("reporter-";"")' in.json > out.txt

CodePudding user response:

You can use split such as

jq -r '.values[2].name | split("-")[1]' response.json

Demo

Edit : Alternatively you can use

jq -r '.values[].name | select(.|split("-")[0]=="reporter")|split("-")[1]' response.json > outfile.txt

without knowing the order of the name element within the array

Demo

  •  Tags:  
  • Related