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;
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
|
show 2 more comments
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
1
Why does thejsonFile.jsoncontain a function? You should fix the problem at its origin, and that would be to ensure thatjsonFile.jsoncontains only valid data.
– t.niese
Mar 26 at 9:06
You can useeval,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
|
show 2 more comments
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
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
javascript json
asked Mar 26 at 9:01
FjodorFjodor
1372 silver badges14 bronze badges
1372 silver badges14 bronze badges
1
Why does thejsonFile.jsoncontain a function? You should fix the problem at its origin, and that would be to ensure thatjsonFile.jsoncontains only valid data.
– t.niese
Mar 26 at 9:06
You can useeval,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
|
show 2 more comments
1
Why does thejsonFile.jsoncontain a function? You should fix the problem at its origin, and that would be to ensure thatjsonFile.jsoncontains only valid data.
– t.niese
Mar 26 at 9:06
You can useeval,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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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');
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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');
add a comment |
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');
add a comment |
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');
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');
answered Mar 26 at 9:08
QwertiyQwertiy
7,9435 gold badges25 silver badges68 bronze badges
7,9435 gold badges25 silver badges68 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Why does the
jsonFile.jsoncontain a function? You should fix the problem at its origin, and that would be to ensure thatjsonFile.jsoncontains 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