duminică, aprilie 22, 2018

How to transform JSON Parent, Child to CSV or TSV

Here is the Javascript code:

```javascript
const json ={
  "objects": [{
      "poolname": "Pool One",
      "ip": "10.10.10.10",
      "groups": [{
          "groupname": "Pool 1 Group 1",
          "ip": "100.100.100.100"
      }, {
          "groupname": "Pool 1 Group 2",
          "ip": "150.150.150.150"   
      }]
    },
    {
      "poolname": "Pool Two",
      "ip": "20.20.20.20",
      "groups": [{
          "groupname": "Pool 2 Group 1",
          "ip": "200.200.200.200"
      }, {
          "groupname": "Pool 2 Group 2",
          "ip": "250.250.250.250"   
      }]
    }]
}

const mapOfMap = (separator)=>[].concat(...json.objects.map(o=>o.groups.map(g=>o.poolname+separator+o.ip+separator+g.groupname+separator+g.ip)))
const toLines = l => l.reduce((acc,v)=>acc+v+"\r\n",'')
const csv = toLines(mapOfMap(','))
const tsv = toLines(mapOfMap('\t'))
console.log(csv)
console.log(tsv)
```

also see the ramda share https://goo.gl/EAM36j where you can run the above code

Niciun comentariu:

Trimiteți un comentariu

Membri

Comentarii