3

I have some data that end user wants in a CSV file. Each entry has a parent node and zero or more child nodes. For example, a parent node might contain:

Name, Id, Date

While a child node might contain:

Name, ChildId

So what I am searching for is a standard to represent multi-level data in CSV. In XML, I can easily create sub nodes. What is the best way to do this in CSV? I want to create a script to extract this data without any confusion about what is parent data and what is child data.

In XML this might look like:

<Parent Name="name1">
<Child Name="ChildName1"></Child>
<Child Name="ChildName2"></Child>
</Parent>

<Parent Name="name2">
<Child Name="ChildName1"></Child>
<Child Name="ChildName2"></Child>
</Parent>
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
serializer
  • 1,003
  • 2
  • 13
  • 27
  • It appears that each would be a row in your CSV and each would be a column. – Nathan Romano Oct 03 '12 at 14:04
  • So, what is your suggestion, that I use different delimiters? Please give me one example of a row in comma separated format with 2 childs in it. – serializer Oct 03 '12 at 14:06
  • Thanks, maybe you meant the same thing but I ended up using the same row like this: ParentColumn1 ParentColumn2 ChildColumn1 ChildColumn2 – serializer Oct 24 '12 at 17:31

2 Answers2

2

The XML could be minimized to this:

<names>
 <name1>
  <childName1/>
  <childName2/>
 </name1>

 <name2>
  <childName1/>
  <childName2/>
 </name2>
</names>

And the CSV to this:

name1  ChildName1
name1  ChildName2
name2  ChildName1
name2  ChildName2

with a JSON serialization like this:

{"names":
 [
  {
  "name1":
   [
    {
    "childName1":""
    },
    {
    "childName2":""
    }
   ]
  },
  {
  "name2":
   [
    {
    "childName1":""
    },
    {
    "childName2":""
    }
   ]
  }
 ]
}

In a row-oriented fashion, the XML could be:

<names>
 <name1 name="ChildName1|ChildName2">
 <name2 name="ChildName1|ChildName2">
</names>

And the corresponding CSV:

name1  ChildName1|ChildName2
name2  ChildName1|ChildName2

And the corresponding JSON:

{"names": [{"name1":"ChildName1|ChildName2"},{"name2":"ChildName1|ChildName2"}]}

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
-1

Please check out Multi-level CSV https://github.com/siara-cc/csv_ml

It allows hierarchical multiple level data representation using CSV and also has reference implementations for java and javascript to parse into JSON and XML DOM.

Disclaimer: I am the author of this method and library

Arundale Ramanathan
  • 1,781
  • 1
  • 18
  • 25