0

I have an array of JSon as follows:

 [
  {
   city: "c1"
   spots: ["s1","s2"]
   tags:  ["t1","t2"]
  },
  {
   city: "c2"
   spots: ["s3","s2"]
   tags:  ["t1","t2","t4"]
  },
  ....
  ....
 ]

I try to convert it and save to csv file by using "json2csv" module. But the problem is i have two fields, "spots" and "tags", are array. It seems with "json2csv", unwindpath option only specify 1 column, not multiple columns.

Is there any module I could use for this task?

arslan
  • 2,034
  • 7
  • 34
  • 61
  • 1. That's not JSON. 2. Even if it *started out* as JSON, it wouldn't be JSON by the time you were turning its contents into a CSV. JSON is a *textual notation* for data exchange. [(More)](http://stackoverflow.com/a/2904181/157247). If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Apr 12 '17 at 08:55

2 Answers2

0

if you expect this:

city;spot;tags
c1;s1;t1
c1;s1:t2
c1;s1;t1
c1;s2;t2
c2;s3;t1
c2;s3;t2
c2;s3;t4
c2;s2;t1
c2;s2;t2
c2;s2;t4

make a nested loop where you fetch the object-keys in a table-header array and write the data out in the innermost loop.

George Moik
  • 445
  • 4
  • 10
0

I solved the problem by using "json-csv" module which has a filter option for columns. By assigning a filter function, I converted the arrays to a single string such as "s1|s2|s3".

arslan
  • 2,034
  • 7
  • 34
  • 61