Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate items json array

I would like to ask for help.
I have json array.
How can I make iterating array after split and create csv output

My input is…

[
    {
        "body": [
            {
                "title": "topinfo",
                "longText": "item1|details item 1|details sdfdfdfd ||details gbgfgghmhjmh||details 5348786"
            },
            {
                "title": "topinfo",
                "longText": "item2|details item 2|details sdfdfdfd ||details gbgfgghmhjmh||details 9784561"
            }
            ]
    }
]

My actually call for jq …

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jq '.[] | [.body[] | {longText,title} ] | to_entries | map( (.value.level = "\(1+.key)" ) | .value) | .[] | [.title,.level,(.longText|split("|"))] '

Actually result

[
  "topinfo",
  "1",
  [
    "item1",
    "details item 1",
    "details sdfdfdfd",
    "details gbgfgghmhjmh",
    "details 5348786"
  ]
]
[
  "topinfo",
  "2",
  [
    "item2",
    "details item 2",
    "details sdfdfdfd",
    "details gbgfgghmhjmh",
    "details 9784561"
  ]
]

And I would like to expect this csv…

topinfo;1;1;item1
topinfo;1;2;details item 1
topinfo;1;3;details sdfdfdfd
topinfo;1;4;details gbgfgghmhjmh
topinfo;1;5;details 5348786

topinfo;2;1;item2
topinfo;2;2;details item 2
topinfo;2;3;details sdfdfdfd
topinfo;2;4;details gbgfgghmhjmh
topinfo;2;5;details 9784561

>Solution :

Nesting iterations and variable bindings:

.[].body                                                   # top-level iteration
| to_entries[] as {key: $i, value: {$title, $longText}}    # collect values
| $longText / "|"                                          # split at "|"
| map(select(. != ""))                                     # remove empty items(?)
| (                                                        # open grouping context
    to_entries[] as {key: $j, $value}                      # collect values
    | [$title, $i+1, $j+1, $value]                         # prepare output line
    | join(";")                                            # join to string
  ), ""                                                    # add empty line
topinfo;1;1;item1
topinfo;1;2;details item 1
topinfo;1;3;details sdfdfdfd 
topinfo;1;4;details gbgfgghmhjmh
topinfo;1;5;details 5348786

topinfo;2;1;item2
topinfo;2;2;details item 2
topinfo;2;3;details sdfdfdfd 
topinfo;2;4;details gbgfgghmhjmh
topinfo;2;5;details 9784561

Demo

To properly escape for a CSV output, use @csv instead of join(";"). Note that this would "join" by comma instead of semicolon, and quote the items if necessary:

"topinfo",1,1,"item1"
"topinfo",1,2,"details item 1"
"topinfo",1,3,"details sdfdfdfd "
"topinfo",1,4,"details gbgfgghmhjmh"
"topinfo",1,5,"details 5348786"

"topinfo",2,1,"item2"
"topinfo",2,2,"details item 2"
"topinfo",2,3,"details sdfdfdfd "
"topinfo",2,4,"details gbgfgghmhjmh"
"topinfo",2,5,"details 9784561"

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading