Ad

Ignoring Function In JSON With JavaScript Using JSON.parse()

- 1 answer

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!

Ad

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');
Ad
source: stackoverflow.com
Ad