Outputting objects from json in csv file









up vote
1
down vote

favorite












I'm trying to find a way to output objects json and save them in csv file, i was trying to use loop 'for in' in 'loop for ' but the problem is that property and length object are different



data json:

[
"name":"Googlebot","htmlimports":true,"objectfit":true,"geolocation":true,"histor":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"Bing","htmlimports":false,"geolocation":true,"history":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"iE","htmlimports":true,"svgclippaths":true,"geolocation":true,"history":true,"ie8compat":false,"strictmode":true,"es5string":true,"es5syntax":true ]


const stringify = require('csv-stringify');
const fs = require('fs')

fs.readFile('./googleBot.json','utf8', (err, dataa) =>
if (err) throw err;
const dates = JSON.parse(dataa)

let data = ;
let columns =
value: 'value',
Googlebot: 'Googlebot',
Bing: 'Bing',
iE: 'iE',
;

for(i = 0; i < dates.length; i++)
for (var prop in dates)
data.push([prop, `$dates[0][prop]`, `$dates[1][prop]`, `$dates[2][prop]`]);




stringify(data, header: true, columns: columns , (err, output) =>
if (err) throw err;
fs.writeFile('my.csv', output, (err) =>
if (err) throw err;
console.log('my.csv saved.');
);
);
);


expected result:



enter image description here










share|improve this question























  • the expected result doesn't seem to bear much relation to the input JSON...there are no "autocomplete", "multiple", "min" or "max" properties in any of your object. And it's unclear how any of this constitutes "comparing" two objects...you simply seem to be want to work through each one and outputting the value of some of the properties.
    – ADyson
    Nov 9 at 10:30










  • Your question is not clear? Your sample data and your result image aren't match
    – Milad Aghamohammadi
    Nov 9 at 10:44











  • I mean expect result is just a example
    – tobi1512
    Nov 9 at 10:47










  • ye it is more like going through each value and outputting than comparing you right
    – tobi1512
    Nov 9 at 11:21










  • "expect result is just a example" ok but it would be a lot more easy for us to understand if it was an example based on what you'd expect the input data you've shown to produce. Otherwise we can't exactly be sure what the rules are. Right now it looks random.
    – ADyson
    Nov 9 at 12:23















up vote
1
down vote

favorite












I'm trying to find a way to output objects json and save them in csv file, i was trying to use loop 'for in' in 'loop for ' but the problem is that property and length object are different



data json:

[
"name":"Googlebot","htmlimports":true,"objectfit":true,"geolocation":true,"histor":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"Bing","htmlimports":false,"geolocation":true,"history":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"iE","htmlimports":true,"svgclippaths":true,"geolocation":true,"history":true,"ie8compat":false,"strictmode":true,"es5string":true,"es5syntax":true ]


const stringify = require('csv-stringify');
const fs = require('fs')

fs.readFile('./googleBot.json','utf8', (err, dataa) =>
if (err) throw err;
const dates = JSON.parse(dataa)

let data = ;
let columns =
value: 'value',
Googlebot: 'Googlebot',
Bing: 'Bing',
iE: 'iE',
;

for(i = 0; i < dates.length; i++)
for (var prop in dates)
data.push([prop, `$dates[0][prop]`, `$dates[1][prop]`, `$dates[2][prop]`]);




stringify(data, header: true, columns: columns , (err, output) =>
if (err) throw err;
fs.writeFile('my.csv', output, (err) =>
if (err) throw err;
console.log('my.csv saved.');
);
);
);


expected result:



enter image description here










share|improve this question























  • the expected result doesn't seem to bear much relation to the input JSON...there are no "autocomplete", "multiple", "min" or "max" properties in any of your object. And it's unclear how any of this constitutes "comparing" two objects...you simply seem to be want to work through each one and outputting the value of some of the properties.
    – ADyson
    Nov 9 at 10:30










  • Your question is not clear? Your sample data and your result image aren't match
    – Milad Aghamohammadi
    Nov 9 at 10:44











  • I mean expect result is just a example
    – tobi1512
    Nov 9 at 10:47










  • ye it is more like going through each value and outputting than comparing you right
    – tobi1512
    Nov 9 at 11:21










  • "expect result is just a example" ok but it would be a lot more easy for us to understand if it was an example based on what you'd expect the input data you've shown to produce. Otherwise we can't exactly be sure what the rules are. Right now it looks random.
    – ADyson
    Nov 9 at 12:23













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to find a way to output objects json and save them in csv file, i was trying to use loop 'for in' in 'loop for ' but the problem is that property and length object are different



data json:

[
"name":"Googlebot","htmlimports":true,"objectfit":true,"geolocation":true,"histor":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"Bing","htmlimports":false,"geolocation":true,"history":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"iE","htmlimports":true,"svgclippaths":true,"geolocation":true,"history":true,"ie8compat":false,"strictmode":true,"es5string":true,"es5syntax":true ]


const stringify = require('csv-stringify');
const fs = require('fs')

fs.readFile('./googleBot.json','utf8', (err, dataa) =>
if (err) throw err;
const dates = JSON.parse(dataa)

let data = ;
let columns =
value: 'value',
Googlebot: 'Googlebot',
Bing: 'Bing',
iE: 'iE',
;

for(i = 0; i < dates.length; i++)
for (var prop in dates)
data.push([prop, `$dates[0][prop]`, `$dates[1][prop]`, `$dates[2][prop]`]);




stringify(data, header: true, columns: columns , (err, output) =>
if (err) throw err;
fs.writeFile('my.csv', output, (err) =>
if (err) throw err;
console.log('my.csv saved.');
);
);
);


expected result:



enter image description here










share|improve this question















I'm trying to find a way to output objects json and save them in csv file, i was trying to use loop 'for in' in 'loop for ' but the problem is that property and length object are different



data json:

[
"name":"Googlebot","htmlimports":true,"objectfit":true,"geolocation":true,"histor":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"Bing","htmlimports":false,"geolocation":true,"history":true,"es5object":true,"strictmode":true,"es5string":true,

"name":"iE","htmlimports":true,"svgclippaths":true,"geolocation":true,"history":true,"ie8compat":false,"strictmode":true,"es5string":true,"es5syntax":true ]


const stringify = require('csv-stringify');
const fs = require('fs')

fs.readFile('./googleBot.json','utf8', (err, dataa) =>
if (err) throw err;
const dates = JSON.parse(dataa)

let data = ;
let columns =
value: 'value',
Googlebot: 'Googlebot',
Bing: 'Bing',
iE: 'iE',
;

for(i = 0; i < dates.length; i++)
for (var prop in dates)
data.push([prop, `$dates[0][prop]`, `$dates[1][prop]`, `$dates[2][prop]`]);




stringify(data, header: true, columns: columns , (err, output) =>
if (err) throw err;
fs.writeFile('my.csv', output, (err) =>
if (err) throw err;
console.log('my.csv saved.');
);
);
);


expected result:



enter image description here







javascript node.js json csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 11:59









mihai

22.3k63968




22.3k63968










asked Nov 9 at 10:26









tobi1512

184




184











  • the expected result doesn't seem to bear much relation to the input JSON...there are no "autocomplete", "multiple", "min" or "max" properties in any of your object. And it's unclear how any of this constitutes "comparing" two objects...you simply seem to be want to work through each one and outputting the value of some of the properties.
    – ADyson
    Nov 9 at 10:30










  • Your question is not clear? Your sample data and your result image aren't match
    – Milad Aghamohammadi
    Nov 9 at 10:44











  • I mean expect result is just a example
    – tobi1512
    Nov 9 at 10:47










  • ye it is more like going through each value and outputting than comparing you right
    – tobi1512
    Nov 9 at 11:21










  • "expect result is just a example" ok but it would be a lot more easy for us to understand if it was an example based on what you'd expect the input data you've shown to produce. Otherwise we can't exactly be sure what the rules are. Right now it looks random.
    – ADyson
    Nov 9 at 12:23

















  • the expected result doesn't seem to bear much relation to the input JSON...there are no "autocomplete", "multiple", "min" or "max" properties in any of your object. And it's unclear how any of this constitutes "comparing" two objects...you simply seem to be want to work through each one and outputting the value of some of the properties.
    – ADyson
    Nov 9 at 10:30










  • Your question is not clear? Your sample data and your result image aren't match
    – Milad Aghamohammadi
    Nov 9 at 10:44











  • I mean expect result is just a example
    – tobi1512
    Nov 9 at 10:47










  • ye it is more like going through each value and outputting than comparing you right
    – tobi1512
    Nov 9 at 11:21










  • "expect result is just a example" ok but it would be a lot more easy for us to understand if it was an example based on what you'd expect the input data you've shown to produce. Otherwise we can't exactly be sure what the rules are. Right now it looks random.
    – ADyson
    Nov 9 at 12:23
















the expected result doesn't seem to bear much relation to the input JSON...there are no "autocomplete", "multiple", "min" or "max" properties in any of your object. And it's unclear how any of this constitutes "comparing" two objects...you simply seem to be want to work through each one and outputting the value of some of the properties.
– ADyson
Nov 9 at 10:30




the expected result doesn't seem to bear much relation to the input JSON...there are no "autocomplete", "multiple", "min" or "max" properties in any of your object. And it's unclear how any of this constitutes "comparing" two objects...you simply seem to be want to work through each one and outputting the value of some of the properties.
– ADyson
Nov 9 at 10:30












Your question is not clear? Your sample data and your result image aren't match
– Milad Aghamohammadi
Nov 9 at 10:44





Your question is not clear? Your sample data and your result image aren't match
– Milad Aghamohammadi
Nov 9 at 10:44













I mean expect result is just a example
– tobi1512
Nov 9 at 10:47




I mean expect result is just a example
– tobi1512
Nov 9 at 10:47












ye it is more like going through each value and outputting than comparing you right
– tobi1512
Nov 9 at 11:21




ye it is more like going through each value and outputting than comparing you right
– tobi1512
Nov 9 at 11:21












"expect result is just a example" ok but it would be a lot more easy for us to understand if it was an example based on what you'd expect the input data you've shown to produce. Otherwise we can't exactly be sure what the rules are. Right now it looks random.
– ADyson
Nov 9 at 12:23





"expect result is just a example" ok but it would be a lot more easy for us to understand if it was an example based on what you'd expect the input data you've shown to produce. Otherwise we can't exactly be sure what the rules are. Right now it looks random.
– ADyson
Nov 9 at 12:23













1 Answer
1






active

oldest

votes

















up vote
0
down vote













Because your objects can contain different properties, you first need to gather a list of all possible properties.



You can do this by creating a temporary array to hold properties, and iterating over your objects and pushing properties in this array, if they're not already there.



var allProps = ;

for (var i = 0; i < dates.length; i++)
for (var prop in dates[i])
if (!allProps.includes(prop))
allProps.push(prop);





After that, it's just a matter of constructing your rows properly:



for (var i = 0; i < allProps.length; i++) 
var prop = allProps[i];
if (prop == 'name') continue; //skip the name property, it's alrady in the columns

var row =
row.push(prop); //first entry in the row is the property name

for (var k = 0; k < dates.length; k++)
row.push(`$dates[k][prop]`);


data.push(row);



See this code running here



Output:



value,Googlebot,Bing,iE
htmlimports,true,false,true
objectfit,true,undefined,undefined
geolocation,true,true,true
histor,true,undefined,undefined
es5object,true,true,undefined
strictmode,true,true,true
es5string,true,true,true
history,undefined,true,true
svgclippaths,undefined,undefined,true
ie8compat,undefined,undefined,false
es5syntax,undefined,undefined,true





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223947%2foutputting-objects-from-json-in-csv-file%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Because your objects can contain different properties, you first need to gather a list of all possible properties.



    You can do this by creating a temporary array to hold properties, and iterating over your objects and pushing properties in this array, if they're not already there.



    var allProps = ;

    for (var i = 0; i < dates.length; i++)
    for (var prop in dates[i])
    if (!allProps.includes(prop))
    allProps.push(prop);





    After that, it's just a matter of constructing your rows properly:



    for (var i = 0; i < allProps.length; i++) 
    var prop = allProps[i];
    if (prop == 'name') continue; //skip the name property, it's alrady in the columns

    var row =
    row.push(prop); //first entry in the row is the property name

    for (var k = 0; k < dates.length; k++)
    row.push(`$dates[k][prop]`);


    data.push(row);



    See this code running here



    Output:



    value,Googlebot,Bing,iE
    htmlimports,true,false,true
    objectfit,true,undefined,undefined
    geolocation,true,true,true
    histor,true,undefined,undefined
    es5object,true,true,undefined
    strictmode,true,true,true
    es5string,true,true,true
    history,undefined,true,true
    svgclippaths,undefined,undefined,true
    ie8compat,undefined,undefined,false
    es5syntax,undefined,undefined,true





    share|improve this answer
























      up vote
      0
      down vote













      Because your objects can contain different properties, you first need to gather a list of all possible properties.



      You can do this by creating a temporary array to hold properties, and iterating over your objects and pushing properties in this array, if they're not already there.



      var allProps = ;

      for (var i = 0; i < dates.length; i++)
      for (var prop in dates[i])
      if (!allProps.includes(prop))
      allProps.push(prop);





      After that, it's just a matter of constructing your rows properly:



      for (var i = 0; i < allProps.length; i++) 
      var prop = allProps[i];
      if (prop == 'name') continue; //skip the name property, it's alrady in the columns

      var row =
      row.push(prop); //first entry in the row is the property name

      for (var k = 0; k < dates.length; k++)
      row.push(`$dates[k][prop]`);


      data.push(row);



      See this code running here



      Output:



      value,Googlebot,Bing,iE
      htmlimports,true,false,true
      objectfit,true,undefined,undefined
      geolocation,true,true,true
      histor,true,undefined,undefined
      es5object,true,true,undefined
      strictmode,true,true,true
      es5string,true,true,true
      history,undefined,true,true
      svgclippaths,undefined,undefined,true
      ie8compat,undefined,undefined,false
      es5syntax,undefined,undefined,true





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Because your objects can contain different properties, you first need to gather a list of all possible properties.



        You can do this by creating a temporary array to hold properties, and iterating over your objects and pushing properties in this array, if they're not already there.



        var allProps = ;

        for (var i = 0; i < dates.length; i++)
        for (var prop in dates[i])
        if (!allProps.includes(prop))
        allProps.push(prop);





        After that, it's just a matter of constructing your rows properly:



        for (var i = 0; i < allProps.length; i++) 
        var prop = allProps[i];
        if (prop == 'name') continue; //skip the name property, it's alrady in the columns

        var row =
        row.push(prop); //first entry in the row is the property name

        for (var k = 0; k < dates.length; k++)
        row.push(`$dates[k][prop]`);


        data.push(row);



        See this code running here



        Output:



        value,Googlebot,Bing,iE
        htmlimports,true,false,true
        objectfit,true,undefined,undefined
        geolocation,true,true,true
        histor,true,undefined,undefined
        es5object,true,true,undefined
        strictmode,true,true,true
        es5string,true,true,true
        history,undefined,true,true
        svgclippaths,undefined,undefined,true
        ie8compat,undefined,undefined,false
        es5syntax,undefined,undefined,true





        share|improve this answer












        Because your objects can contain different properties, you first need to gather a list of all possible properties.



        You can do this by creating a temporary array to hold properties, and iterating over your objects and pushing properties in this array, if they're not already there.



        var allProps = ;

        for (var i = 0; i < dates.length; i++)
        for (var prop in dates[i])
        if (!allProps.includes(prop))
        allProps.push(prop);





        After that, it's just a matter of constructing your rows properly:



        for (var i = 0; i < allProps.length; i++) 
        var prop = allProps[i];
        if (prop == 'name') continue; //skip the name property, it's alrady in the columns

        var row =
        row.push(prop); //first entry in the row is the property name

        for (var k = 0; k < dates.length; k++)
        row.push(`$dates[k][prop]`);


        data.push(row);



        See this code running here



        Output:



        value,Googlebot,Bing,iE
        htmlimports,true,false,true
        objectfit,true,undefined,undefined
        geolocation,true,true,true
        histor,true,undefined,undefined
        es5object,true,true,undefined
        strictmode,true,true,true
        es5string,true,true,true
        history,undefined,true,true
        svgclippaths,undefined,undefined,true
        ie8compat,undefined,undefined,false
        es5syntax,undefined,undefined,true






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 11:49









        mihai

        22.3k63968




        22.3k63968



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223947%2foutputting-objects-from-json-in-csv-file%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線