I am trying to require() this JSON file.
{
"info" : function (request) {
var i = "<pre>";
i+= "current working directory: " + process.cwd() + "\n";
i+="request url: " + request.url + "\n";
i+= "</pre>";
return i;
}
}
Using this line
var render = require('./render.json');
But I get an exception from the JSON file of : Unexpected token u
What am I doing wrong please ?
The following works in a browser. Which I would expect, since a function is a object. And nodejs docs suggests JSON can be a module: .html#modules_file_modules
<html>
<head>
</head>
<body>
<script>
var a = {
"b" : function(msg){
alert(msg);
}
}
a.b("Hello");
</script>
</body>
</html>
I am trying to require() this JSON file.
{
"info" : function (request) {
var i = "<pre>";
i+= "current working directory: " + process.cwd() + "\n";
i+="request url: " + request.url + "\n";
i+= "</pre>";
return i;
}
}
Using this line
var render = require('./render.json');
But I get an exception from the JSON file of : Unexpected token u
What am I doing wrong please ?
The following works in a browser. Which I would expect, since a function is a object. And nodejs docs suggests JSON can be a module: http://nodejs/api/modules.html#modules_file_modules
<html>
<head>
</head>
<body>
<script>
var a = {
"b" : function(msg){
alert(msg);
}
}
a.b("Hello");
</script>
</body>
</html>
Share
Improve this question
edited Sep 30, 2013 at 10:50
Anthony Scaife
asked Sep 28, 2013 at 0:07
Anthony ScaifeAnthony Scaife
5645 silver badges15 bronze badges
5
- Can you show the JSON file? – thefourtheye Commented Sep 28, 2013 at 0:14
- is there a line number given (in the CSV)? – feeela Commented Sep 28, 2013 at 0:21
- 2 unexpected token "u" is because the only valid token after "f" is "a" (for "false") – Andrey Sidorov Commented Sep 28, 2013 at 0:56
- That makes perfect sense @andrey-sidorov. I am new to nodejs, so maybe modules are not what I think they are. – Anthony Scaife Commented Oct 1, 2013 at 12:06
-
1
You're trying to mix JSON and JS here. The two languages are actually distinct from one another, despite sharing very similar syntax. JSON is basically the specific subset of JS syntax that allows you to define certain data structures - strings, booleans, numbers, Objects, and Arrays. Note that functions are not part of this! JS, on the other hand is... well.. everything else. Your
render.json
file isn't valid JSON or JS. Thus, there's no way to interpret it that makes sense in either context! tl;dr - functions aren't valid JSON. – broofa Commented Nov 27, 2014 at 12:56
2 Answers
Reset to default 4JSON is purely meant to be a data description language. Per http://www.json, it is a "lightweight data-interchange format." - not a programming language.
you cannot have function inside your JSON and use node.
{
"error": [
function (request) {
}
]
}
Is it valid to define functions in JSON results?
The way I have gotten around this is to create a js file instead of a json file:
config.js
exports.config = [
{
"foo": 12,
"bar": 10,
"baz": function(a){
console.dir(a);
}
}
]
then within node:
config = require('./config.js').config;
var a = {
m: 'something',
o: 'somethingelses'
}
config[0].baz(a);