serializer
serializer

Reputation: 1013

Advice for multi level csv file creation

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>

Upvotes: 3

Views: 1771

Answers (2)

Arundale Ramanathan
Arundale Ramanathan

Reputation: 2079

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

Upvotes: -1

Paul Sweatte
Paul Sweatte

Reputation: 24617

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

Upvotes: 2

Related Questions