-1

Here is my sample code:

[
    {
        "urlString" : "https://www.test.com/products/gift-card",
        "imageUrl" : "https://cdn.shopify.com/s/files/1/0454/6253/products/dope_mone_850x.jpg?v=1539288792"
    },
    {
        "urlString" : "https://www.test.com/products/gift-card",
        "imageUrl" : "https://cdn.shopify.com/s/files/1/0454/6253/products/dope_mone_850x.jpg?v=1539288792"
    },
]

I want to create a CSV file with the output:

https://www.test.com/products/gift-card,https://cdn.shopify.com/s/files/1/0454/6253/products/dope_mone_850x.jpg?v=1539288792
https://www.test.com/products/gift-card,https://cdn.shopify.com/s/files/1/0454/6253/products/dope_mone_850x.jpg?v=1539288792

Header tags are not essential but if they are in the solution thats fine.

thanks for the help!

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Kingswoop
  • 148
  • 8
  • Ideally, one would show what you've tried and how the output differed from expectations. – Charles Duffy Feb 20 '20 at 01:51
  • Also, make sure that even when you trim your JSON down you don't make it invalid. In this case, it doesn't parse on account of an extra comma. – Charles Duffy Feb 20 '20 at 01:53
  • BTW, you're showing output that doesn't have quotes around the strings. Is that intentional? (*Real* CSV should accept those quotes, and some cases -- like when your keys or values contain literal commas -- even require them; but if you're building for consumption of a different tool, maybe we need to change things a bit). – Charles Duffy Feb 20 '20 at 01:54

1 Answers1

1
jq -r '.[] | [.urlString, .imageUrl] | @csv'

See this on jqplay at https://jqplay.org/s/zL6HpSSFw0

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441