web stats
JSON to CSV - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2018, 02:29 PM
yeruva yeruva is offline
Mirth Newb
 
Join Date: Nov 2017
Location: Dallas, USA
Posts: 21
yeruva is on a distinguished road
Default JSON to CSV

I have javascript code convert JSON to CSV for displaying corresponding elements in single row. However expected results should array elements should display in each row.

JSON
{
"id": "123",
"name": "foo",
"emails":[
{
"id": "123",
"address": "somethinghere"
},
{
"id": "456",
"address": "something"
}
]
}

CSV
id name emails__id emails__address
123 foo 123 somethinghere
456 something

JAVA Script
if (msg.trim().charAt(0)=='[')
{ var jsonObject=JSON.parse(msg);}
else
{var jsonObject=JSON.parse('[' + msg + ']');}

var msg1=msg.slice(msg.search(":")+1, msg.length-1);
var num = msg1.search("{")+1;

if (num==8.0)
{var jsonObject=JSON.parse(msg1);}

msg = <delimited/>;
var counter = 0;
for each (element in jsonObject)
{
var row=<row/>;
var col=<row/>;
for (var key1 in element)
{if (typeof element[key1] != 'object')
{
row[key1]=element[key1].toString();
col[key1]=key1;
}
for (var key2 in element[key1])
{if (typeof element[key1][key2] != 'object')
{
row[key1+key2]=element[key1][key2].toString();
col[key1+key2]=key1+'/'+key2;
}

for (var key3 in element[key1][key2])
{if (typeof element[key1][key2][key3] != 'object')
{
row[key1+key2+key3]=element[key1][key2][key3].toString();
col[key1+key2+key3]=key1+'/'+key2+'/'+key3;
}
}
}

}

counter = counter + 1;
if(counter == 1)
{msg.appendChild(col);}
msg.appendChild(row);
}

Last edited by yeruva; 06-12-2018 at 02:34 PM.
Reply With Quote
Reply

Tags
json to csv

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 02:12 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Mirth Corporation