Retrieve values from JSON nested arrays using jq

Asked 2 years ago, Updated 2 years ago, 102 views

I have converted the following JSON from jq to CSV.

{
    "items": [
        {
            "id": "1",
            "name": "masao",
            "images": ["001.jpg", "002.jpg" ]    
        },
        {
            "id": "2",
            "name": "video",
            "images": ["003.jpg", "004.jpg" ]    
        }
    ]
}

I create a CSV using the command below, but I don't know how to get information about images nested in an array.

cat sample.json | jq-r'.items [ ] | [ .id, .name ] | @csv' | sed-e's /"//g';
1,masao
2, video

What commands should I enter?
1,masao,001.jpg
2,video,003.jpg
Is it possible to retrieve it like this?

json csv jq

2022-09-30 17:27

1 Answers

I was told in the comments.

cat sample.json | jq-r'.items[] | [.id, .name, images[0]] | @csv' | sed-e's /" //g';

Also, without using sed-e's /"//g';

cat sample.json|jq-r'.items[]|"\(.id), \(.name), \(.images[0])"'

If you write that, you can remove the double quotation.It was very helpful.


2022-09-30 17:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.