0

I have a json output

{
  "code": 200,
  "success": true,
  "data": [{
      "id": "66",
      "subnet": "0.0.0.0",
      "mask": "",
      "sectionId": "1",
      "description": "THA4",
    },
    {
      "id": "72",
      "subnet": "1.2.3.4",
      "mask": "",
      "sectionId": "1",
      "description": "ALF5",
    },
],
}

I'd like to have the output like this based on the description and subnet:

THA4  
0.0.0.0  
ALF5  
1.2.3.4

and want to have like this too:

THA4,0.0.0.0  
ALF5,1.2.3.4 

With my query:

jq '.data | .[].description,.[].subnet'  

I can get only like this:

0.0.0.0  
1.2.3.4  
THA4  
ALF5 

1 Answer 1

1
  1. Strictly speaking, the sample "JSON" is not valid JSON. The following assumes that the superfluous commas have been removed.

  2. To obtain the desired linear listing, just hoist the []:

    jq -r '.data[] | (.description, .subnet)'

produces:

THA4
0.0.0.0
ALF5
1.2.3.4
  1. One way to get CSV is to use @csv:

    .data[] | [.description, .subnet] | @csv

produces quoted values:

"THA4","0.0.0.0"
"ALF5","1.2.3.4"
  1. If you don't want the individual values to be quoted:

.data[] | "\(.description), \(.subnet)"

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, the 4th one doesn't work, it gives back like this: (.description), (.subnet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.