Ignoring function in JSON with JavaScript using JSON.parse()How do JavaScript closures work?Can comments be used in JSON?var functionName = function() vs function functionName() Which equals operator (== vs ===) should be used in JavaScript comparisons?What is the correct JSON content type?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Why does Google prepend while(1); to their JSON responses?How do I remove a particular element from an array in JavaScript?

What "fuel more powerful than anything the West (had) in stock" put Laika in orbit aboard Sputnik 2?

Create Array from list of indices/values

Credit card details stolen every 1-2 years. What am I doing wrong?

How to make a plagal cadence sound convincing as an ending?

How to check if a new username is a system user?

Migrating Configuration - 3750G to 3750X

Why did Steve Rogers choose this character in Endgame?

What is the meaning of [[:space:]] in bash?

ArcPy Delete Function not working inside for loop?

Is it legal for a supermarket to refuse to sell an adult beer if an adult with them doesn’t have their ID?

Is it okay for a chapter's POV to shift as it progresses?

Playing saxophone without using the octave key

Should I be able to keep my company purchased standing desk when I leave my job?

Alphanumeric Line and Curve Counting

Why does "git status" show I'm on the master branch and "git branch" does not?

Is there an English equivalent for "Les carottes sont cuites", while keeping the vegetable reference?

What are "full piece" and "half piece" in chess?

Is this artwork (used in a video game) real?

Random piece of plastic

Improve quality of image bars

Why do so many pure math PhD students drop out or leave academia, compared to applied mathematics PhDs?

Alternator dying so junk car?

Was Apollo 13 radio blackout on reentry longer than expected?

Will this tire fail its MOT?



Ignoring function in JSON with JavaScript using JSON.parse()


How do JavaScript closures work?Can comments be used in JSON?var functionName = function() vs function functionName() Which equals operator (== vs ===) should be used in JavaScript comparisons?What is the correct JSON content type?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Why does Google prepend while(1); to their JSON responses?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am using Highcharts to create some charts, more specifically I am using https://github.com/highcharts/node-export-server in order to export the charts. A JSON file is used for the source of what the chart will contain(see JSON below).



I then read data from a file and want to replace the data property in the JSON file with what I read from the file, the problem I have is that the JSON contains a function() so when I use JSON.parse() it fails because a JSON cannot contain a function().




"chart":
"type":"bar",
"labels":
"style":
"fontFamily":"Arial",
"fontSize":"14px"
,
"events":
"load": function()
var plotBands = this.xAxis[0].plotLinesAndBands;
for (var i in plotBands)
var d = plotBands[i].svgElem.d;
var dArray = d.split(" ");
var rect = x:25, y:dArray[5], width: 540, height: 100;
this.renderer.rect(rect.x, rect.y, rect.width, rect.height)
.attr(
fill: 'green'
)
.add();



,
"xAxis":
"categories":[
"name":"Group A","categories":["Text A.1","Text A.2","Text A.3"],
"name":"Group B","categories":["Text B.1","Text B.2","Text B.3"],
"name":"Group C","categories":["Text C.1","Text C.2","Text C.3"],
"name":"Group D","categories":["Text D.1","Text D.2","Text D.3"],
"name":"Group E","categories":["Text E.1","Text E.2","Text E.3"],
"name":"Group F","categories":["Text F.1","Text F.2","Text F.3"],
"name":"Group G","categories":["Text G.1","Text G.2","Text G.3"]],
"labels":
"rotation":0,
"style":
"fontFamily":"Arial",
"fontSize":"14px",
"overflow":"justify",
"textOverflow":"none"


,
"yAxis":
"min":0,
"max":100,
"title":"text":"y-values",
"style":
"fontSize":"14px",
"overflow":"justify"

,
"series":[
"type":"bar",
"name":"Score",
"data":[20,20,43,80,20,10,65,88,35,62,48,85,63,20,13,54,34,100,23,55,76],
"color":"red"
]



This is the code line that fails



var m = JSON.parse(fs.readFileSync('jsonFile.json', 'utf-8'));


Is there a way around this issue? Can I ignore the function in some way because I am not interesting in reading its content anyhow? Thanks in advance!










share|improve this question

















  • 1





    Why does the jsonFile.json contain a function? You should fix the problem at its origin, and that would be to ensure that jsonFile.json contains only valid data.

    – t.niese
    Mar 26 at 9:06











  • You can use eval, var m = eval("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");

    – ponury-kostek
    Mar 26 at 9:06












  • @ponury-kostek, your code will throw.

    – Qwertiy
    Mar 26 at 9:09











  • @Qwertiy right, fixed

    – ponury-kostek
    Mar 26 at 9:10











  • You could create your own function that accepts a row of chart data and returns valid JSON.

    – Chris Adams
    Mar 26 at 9:12

















0















I am using Highcharts to create some charts, more specifically I am using https://github.com/highcharts/node-export-server in order to export the charts. A JSON file is used for the source of what the chart will contain(see JSON below).



I then read data from a file and want to replace the data property in the JSON file with what I read from the file, the problem I have is that the JSON contains a function() so when I use JSON.parse() it fails because a JSON cannot contain a function().




"chart":
"type":"bar",
"labels":
"style":
"fontFamily":"Arial",
"fontSize":"14px"
,
"events":
"load": function()
var plotBands = this.xAxis[0].plotLinesAndBands;
for (var i in plotBands)
var d = plotBands[i].svgElem.d;
var dArray = d.split(" ");
var rect = x:25, y:dArray[5], width: 540, height: 100;
this.renderer.rect(rect.x, rect.y, rect.width, rect.height)
.attr(
fill: 'green'
)
.add();



,
"xAxis":
"categories":[
"name":"Group A","categories":["Text A.1","Text A.2","Text A.3"],
"name":"Group B","categories":["Text B.1","Text B.2","Text B.3"],
"name":"Group C","categories":["Text C.1","Text C.2","Text C.3"],
"name":"Group D","categories":["Text D.1","Text D.2","Text D.3"],
"name":"Group E","categories":["Text E.1","Text E.2","Text E.3"],
"name":"Group F","categories":["Text F.1","Text F.2","Text F.3"],
"name":"Group G","categories":["Text G.1","Text G.2","Text G.3"]],
"labels":
"rotation":0,
"style":
"fontFamily":"Arial",
"fontSize":"14px",
"overflow":"justify",
"textOverflow":"none"


,
"yAxis":
"min":0,
"max":100,
"title":"text":"y-values",
"style":
"fontSize":"14px",
"overflow":"justify"

,
"series":[
"type":"bar",
"name":"Score",
"data":[20,20,43,80,20,10,65,88,35,62,48,85,63,20,13,54,34,100,23,55,76],
"color":"red"
]



This is the code line that fails



var m = JSON.parse(fs.readFileSync('jsonFile.json', 'utf-8'));


Is there a way around this issue? Can I ignore the function in some way because I am not interesting in reading its content anyhow? Thanks in advance!










share|improve this question

















  • 1





    Why does the jsonFile.json contain a function? You should fix the problem at its origin, and that would be to ensure that jsonFile.json contains only valid data.

    – t.niese
    Mar 26 at 9:06











  • You can use eval, var m = eval("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");

    – ponury-kostek
    Mar 26 at 9:06












  • @ponury-kostek, your code will throw.

    – Qwertiy
    Mar 26 at 9:09











  • @Qwertiy right, fixed

    – ponury-kostek
    Mar 26 at 9:10











  • You could create your own function that accepts a row of chart data and returns valid JSON.

    – Chris Adams
    Mar 26 at 9:12













0












0








0








I am using Highcharts to create some charts, more specifically I am using https://github.com/highcharts/node-export-server in order to export the charts. A JSON file is used for the source of what the chart will contain(see JSON below).



I then read data from a file and want to replace the data property in the JSON file with what I read from the file, the problem I have is that the JSON contains a function() so when I use JSON.parse() it fails because a JSON cannot contain a function().




"chart":
"type":"bar",
"labels":
"style":
"fontFamily":"Arial",
"fontSize":"14px"
,
"events":
"load": function()
var plotBands = this.xAxis[0].plotLinesAndBands;
for (var i in plotBands)
var d = plotBands[i].svgElem.d;
var dArray = d.split(" ");
var rect = x:25, y:dArray[5], width: 540, height: 100;
this.renderer.rect(rect.x, rect.y, rect.width, rect.height)
.attr(
fill: 'green'
)
.add();



,
"xAxis":
"categories":[
"name":"Group A","categories":["Text A.1","Text A.2","Text A.3"],
"name":"Group B","categories":["Text B.1","Text B.2","Text B.3"],
"name":"Group C","categories":["Text C.1","Text C.2","Text C.3"],
"name":"Group D","categories":["Text D.1","Text D.2","Text D.3"],
"name":"Group E","categories":["Text E.1","Text E.2","Text E.3"],
"name":"Group F","categories":["Text F.1","Text F.2","Text F.3"],
"name":"Group G","categories":["Text G.1","Text G.2","Text G.3"]],
"labels":
"rotation":0,
"style":
"fontFamily":"Arial",
"fontSize":"14px",
"overflow":"justify",
"textOverflow":"none"


,
"yAxis":
"min":0,
"max":100,
"title":"text":"y-values",
"style":
"fontSize":"14px",
"overflow":"justify"

,
"series":[
"type":"bar",
"name":"Score",
"data":[20,20,43,80,20,10,65,88,35,62,48,85,63,20,13,54,34,100,23,55,76],
"color":"red"
]



This is the code line that fails



var m = JSON.parse(fs.readFileSync('jsonFile.json', 'utf-8'));


Is there a way around this issue? Can I ignore the function in some way because I am not interesting in reading its content anyhow? Thanks in advance!










share|improve this question














I am using Highcharts to create some charts, more specifically I am using https://github.com/highcharts/node-export-server in order to export the charts. A JSON file is used for the source of what the chart will contain(see JSON below).



I then read data from a file and want to replace the data property in the JSON file with what I read from the file, the problem I have is that the JSON contains a function() so when I use JSON.parse() it fails because a JSON cannot contain a function().




"chart":
"type":"bar",
"labels":
"style":
"fontFamily":"Arial",
"fontSize":"14px"
,
"events":
"load": function()
var plotBands = this.xAxis[0].plotLinesAndBands;
for (var i in plotBands)
var d = plotBands[i].svgElem.d;
var dArray = d.split(" ");
var rect = x:25, y:dArray[5], width: 540, height: 100;
this.renderer.rect(rect.x, rect.y, rect.width, rect.height)
.attr(
fill: 'green'
)
.add();



,
"xAxis":
"categories":[
"name":"Group A","categories":["Text A.1","Text A.2","Text A.3"],
"name":"Group B","categories":["Text B.1","Text B.2","Text B.3"],
"name":"Group C","categories":["Text C.1","Text C.2","Text C.3"],
"name":"Group D","categories":["Text D.1","Text D.2","Text D.3"],
"name":"Group E","categories":["Text E.1","Text E.2","Text E.3"],
"name":"Group F","categories":["Text F.1","Text F.2","Text F.3"],
"name":"Group G","categories":["Text G.1","Text G.2","Text G.3"]],
"labels":
"rotation":0,
"style":
"fontFamily":"Arial",
"fontSize":"14px",
"overflow":"justify",
"textOverflow":"none"


,
"yAxis":
"min":0,
"max":100,
"title":"text":"y-values",
"style":
"fontSize":"14px",
"overflow":"justify"

,
"series":[
"type":"bar",
"name":"Score",
"data":[20,20,43,80,20,10,65,88,35,62,48,85,63,20,13,54,34,100,23,55,76],
"color":"red"
]



This is the code line that fails



var m = JSON.parse(fs.readFileSync('jsonFile.json', 'utf-8'));


Is there a way around this issue? Can I ignore the function in some way because I am not interesting in reading its content anyhow? Thanks in advance!







javascript json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 9:01









FjodorFjodor

1372 silver badges14 bronze badges




1372 silver badges14 bronze badges







  • 1





    Why does the jsonFile.json contain a function? You should fix the problem at its origin, and that would be to ensure that jsonFile.json contains only valid data.

    – t.niese
    Mar 26 at 9:06











  • You can use eval, var m = eval("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");

    – ponury-kostek
    Mar 26 at 9:06












  • @ponury-kostek, your code will throw.

    – Qwertiy
    Mar 26 at 9:09











  • @Qwertiy right, fixed

    – ponury-kostek
    Mar 26 at 9:10











  • You could create your own function that accepts a row of chart data and returns valid JSON.

    – Chris Adams
    Mar 26 at 9:12












  • 1





    Why does the jsonFile.json contain a function? You should fix the problem at its origin, and that would be to ensure that jsonFile.json contains only valid data.

    – t.niese
    Mar 26 at 9:06











  • You can use eval, var m = eval("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");

    – ponury-kostek
    Mar 26 at 9:06












  • @ponury-kostek, your code will throw.

    – Qwertiy
    Mar 26 at 9:09











  • @Qwertiy right, fixed

    – ponury-kostek
    Mar 26 at 9:10











  • You could create your own function that accepts a row of chart data and returns valid JSON.

    – Chris Adams
    Mar 26 at 9:12







1




1





Why does the jsonFile.json contain a function? You should fix the problem at its origin, and that would be to ensure that jsonFile.json contains only valid data.

– t.niese
Mar 26 at 9:06





Why does the jsonFile.json contain a function? You should fix the problem at its origin, and that would be to ensure that jsonFile.json contains only valid data.

– t.niese
Mar 26 at 9:06













You can use eval, var m = eval("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");

– ponury-kostek
Mar 26 at 9:06






You can use eval, var m = eval("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");

– ponury-kostek
Mar 26 at 9:06














@ponury-kostek, your code will throw.

– Qwertiy
Mar 26 at 9:09





@ponury-kostek, your code will throw.

– Qwertiy
Mar 26 at 9:09













@Qwertiy right, fixed

– ponury-kostek
Mar 26 at 9:10





@Qwertiy right, fixed

– ponury-kostek
Mar 26 at 9:10













You could create your own function that accepts a row of chart data and returns valid JSON.

– Chris Adams
Mar 26 at 9:12





You could create your own function that accepts a row of chart data and returns valid JSON.

– Chris Adams
Mar 26 at 9:12












1 Answer
1






active

oldest

votes


















1














If the file is trusted you may use eval:



var m = (0, eval)("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");


Anyway, I don't recommend you to do so.




You should either remove function and make valid json or change file to js, add module.exports = there and then use normal require on it:



var m = require('jsonFile.js');





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',
    autoActivateHeartbeat: false,
    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%2f55353218%2fignoring-function-in-json-with-javascript-using-json-parse%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If the file is trusted you may use eval:



    var m = (0, eval)("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");


    Anyway, I don't recommend you to do so.




    You should either remove function and make valid json or change file to js, add module.exports = there and then use normal require on it:



    var m = require('jsonFile.js');





    share|improve this answer



























      1














      If the file is trusted you may use eval:



      var m = (0, eval)("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");


      Anyway, I don't recommend you to do so.




      You should either remove function and make valid json or change file to js, add module.exports = there and then use normal require on it:



      var m = require('jsonFile.js');





      share|improve this answer

























        1












        1








        1







        If the file is trusted you may use eval:



        var m = (0, eval)("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");


        Anyway, I don't recommend you to do so.




        You should either remove function and make valid json or change file to js, add module.exports = there and then use normal require on it:



        var m = require('jsonFile.js');





        share|improve this answer













        If the file is trusted you may use eval:



        var m = (0, eval)("(" + fs.readFileSync('jsonFile.json', 'utf-8') + ")");


        Anyway, I don't recommend you to do so.




        You should either remove function and make valid json or change file to js, add module.exports = there and then use normal require on it:



        var m = require('jsonFile.js');






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 9:08









        QwertiyQwertiy

        7,9435 gold badges25 silver badges68 bronze badges




        7,9435 gold badges25 silver badges68 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353218%2fignoring-function-in-json-with-javascript-using-json-parse%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해