1

Suppose we have following file dataset\test.js


var dataset={
HK_NetMovement_Censtad: {
      name: 'HK_NetMovement_Censtad',
      keys: [],
      columns: [{
          id: "dateiso",
          name: {
            en: "Mid_yr"
          },
          type: "datetime"
        },
        {
          id: "value",
          name: {
            en: "Pop_size"
          },
          type: "numeric"
        }
      ],
      fearueServer: 'HK_KEY_VALUE'
    },
    HK_CPIchangeseason_Censtad: {
      fearueServer: 'HK_KEY_VALUE',
      columns: [{
          id: "dateiso",
          name: {
            en: "Month"
          },
          type: "datetime"
        },
        {
          id: "value",
          name: {
            en: "Monthly Rate"
          },
          type: "numeric"
        }
      ],
      keys: [],
      name: 'HK_CPIchangeseason_Censtad'
    }
}

I want to read it as an Object in Nodejs. One way is to read it using eval function as follows:

fs.readFile(filepath, 'utf8', function (err, data) {

eval(data)
console.log(dataset)
})


that works fine. But eval function has security holes. Is there any other workarounds to avoid using eval?

Thanks

Majid Hojati
  • 1,740
  • 4
  • 29
  • 61
  • Why are you reading the file instead of exporting `dataset` and using `require()`? – Dominic Nov 20 '20 at 04:26
  • @Dominic I have a set of files in a folder with similar format and I want to loop over them and convert them into JS object. How can I use `require()`? I want to later be able to update those files or add new files to the folder and be able to read them into js object – Majid Hojati Nov 20 '20 at 04:32
  • 1
    `JSON.parse(data)`? Or am I just dead wrong and missing the point of the question? – Shmack Nov 20 '20 at 04:32
  • @ShanerM13 the input file is not JSON, It is a JS object. it has `var dataset={}` at its begining – Majid Hojati Nov 20 '20 at 04:34
  • 1
    "A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object." https://www.w3schools.com/js/js_json_parse.asp This function should also be available in node, but it takes a string and converts it to a js object... and since you read a file, it stores the data as a string. – Shmack Nov 20 '20 at 04:36
  • @ShanerM13 but it converts JSON objects not such formats. It exists in Node but if we call this function it returns an error `JSON.parse('var a = 1') Uncaught SyntaxError: Unexpected token v in JSON at position 0` which is correct since my input files are not JSON format – Majid Hojati Nov 20 '20 at 04:40
  • Oh... I see you want like a JavascriptObject.parse()... my bad. I think I have a vague memory of seeing something like that... but maybe not... something like https://stackoverflow.com/questions/1086404/string-to-object-in-js ? Probably not... I am curious what the solution is, as I imagine you are. – Shmack Nov 20 '20 at 04:42
  • @ShanerM13 I found a solution, I added the answer. I have to edit files manually but it is just adding a header to them and the rest will be easy – Majid Hojati Nov 20 '20 at 04:51

1 Answers1

0

If I convert those files to the following format

module.exports = function () {
    this.dataset = {
        HK_NetMovement_Censtad: {
            name: 'HK_NetMovement_Censtad',
            keys: [],
            columns: [{
                    id: "dateiso",
                    name: {
                        en: "Mid_yr"
                    },
                    type: "datetime"
                },
                {
                    id: "value",
                    name: {
                        en: "Pop_size"
                    },
                    type: "numeric"
                }
            ],
            fearueServer: 'HK_KEY_VALUE'
        },
        HK_CPIchangeseason_Censtad: {
            fearueServer: 'HK_KEY_VALUE',
            columns: [{
                    id: "dateiso",
                    name: {
                        en: "Month"
                    },
                    type: "datetime"
                },
                {
                    id: "value",
                    name: {
                        en: "Monthly Rate"
                    },
                    type: "numeric"
                }
            ],
            keys: [],
            name: 'HK_CPIchangeseason_Censtad'
        }
}

I can use

  var j = require('./datasets/'+file)

to import them into my file and work with them

Majid Hojati
  • 1,740
  • 4
  • 29
  • 61