Search code examples
yamljqyq

How to convert the yaml to csv with array?


The example:

ProductLine:
  ProductLineName: aa
  ADO_FeedsList:
    - organizationName: bb
      Project:
        - ProjectName: cc
          ProjectFeedsName: 
          - dd
          - ee
        - ProjectName: ff
          ProjectFeedsName:
          - gg
          - hh
  OtherInfo: N/A

I expected the following output:

bb,cc,dd
bb,cc,ee
bb,ff,gg
bb,ff,hh

I have tried :

yq -o csv '.ProductLine.ADO_FeedsList[] |[.organizationName] + (.Project[]|.ProjectName)' test.yaml

It can output:

bb,cc
bb,ff

Then i tried:

yq -o csv '.ProductLine.ADO_FeedsList[] |[.organizationName] + (.Project[]|.ProjectName) + (.Project[]|.ProjectFeedsName[]|[.])' test.yaml

Error: !!seq (ProductLine.ADO_FeedsList.0.Project.0.ProjectFeedsName.0) cannot be added to a !!str (ProductLine.ADO_FeedsList.0.Project.0.ProjectName)

How to write the ProjectFeedsName array command?

I am a yq new user,could you share the method to format this yaml ? Or is there any other way to format this yaml to csv?


Solution

  • When adding arrays, make sure that all parts have brackets:

    yq -o csv '
      .ProductLine.ADO_FeedsList[] | [.organizationName] + (
        .Project[] | [.ProjectName] + (.ProjectFeedsName[] | [.])
      )
    ' test.yaml
    
    bb,cc,dd
    bb,cc,ee
    bb,ff,gg
    bb,ff,hh