Why can't I print this simple json? jsonlint.com says this is valid
json:
[
{
"token_start_offset": "0.00",
"token_duration": "4.00",
"token_base_start_offset": "0.00",
"token_base_duration": "4.00",
"token_type": "background_noise",
"token_background_noise_type": "other",
"session_id": "1459194633575",
"token_base_form": "…",
"token_print_form": "…",
"session_boundary": "begin",
"nonspeech_boundary": "begin",
"token_id": "0"
}
]
app.js:
var testJson = require('./json');
console.log(testJson);
But when I run this, I get the below error:
Error:
module.js:428
throw err;
^
SyntaxError: C:\Users\Owner\Desktop\format test\json.json: Unexpected token
at Object.parse (native)
at Object.Module._extensions..json (module.js:425:27)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\Owner\Desktop\format test\app.js:1:78)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
Windows 10
node -v 4.2.6
Because the JSON parser in Node's require() assumes ASCII characters and your example contains a Unicode character: …. If you replace all instances of … with \u2026, your JSON should parse.
Related
I'm trying to read CSV with Javascript, but I'm getting an error.
Here is my code:
fs.readFile('./data/AMZN.csv', async (err,data)=>{
if(err){
console.log(err)
return
}
console.log(data.replace("$",""));
})
Here is the example of my CSV data:
Date, Close/Last, Volume, Open, High, Low
09/11/2020, $3116.22, 5093982, $3208.69, $3217.3409, $3083.98
09/10/2020, $3175.11, 5330741, $3307.219, $3349.89, $3170.55
09/09/2020, $3268.61, 5188675, $3202.99, $3303.175, $3185
And here is the error that I get:
SyntaxError: Unexpected number
at wrapSafe (internal/modules/cjs/loader.js:1072:16)
at Module._compile (internal/modules/cjs/loader.js:1122:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\jackl\Desktop\Projects\scraper_cheerio\scraper.js:5:14)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
I've figured out that Javascript perceives $3116 as a string and the following .22 as a property of that string object, which can not be a number and therefore throws "Unexpected number".
But how do I fix it? I've been trying to deal with it for 2 days, but no luck so far.
The problem was in this line:
const AMZN = require("./data/AMZN.csv");
The code in the question works fine.
i am trying execute test with jest in CRA and i keep getting this weird error.
C:\Users\mfonpah\Documents\projects\nexus-web-client\web-
client\node_modules\auth0-js\src\index.js:1
(function (exports, require, module, __filename, __dirname) { import
Authentication from './authentication';
^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Object.require.extensions.(anonymous function) [as .js]
(C:\Users\mfonpah\Documents\projects\nexus-web-client\web-
client\node_modules\babel-register\lib\node.js:152:7)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
any ideas on how to fix this?
thanks
If you see this error, your EF6 import statements are not being transpiled. Make sure your babel presets contain "env"
.babelrc and "babel" property in package.json
{
"presets":["env", "react"]
}
Depending on your use case you might also need to add the transform-es2015-modules-amd plugin, more here.
The full error is:
SyntaxError: /root/anon/config.json: Unexpected token ]
at Object.parse (native)
at Object.Module._extensions..json (module.js:425:27)
at Module.load (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at loadJson (/root/anon/anon.coffee:63:3)
at getConfig (/root/anon/anon.coffee:51:12)
at main (/root/anon/anon.coffee:136:12)
at Object.<anonymous> (/root/anon/anon.coffee:147:3)
at Object.<anonymous> (/root/anon/anon.coffee:3:1)
at Module._compile (module.js:410:26)
at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:173:23)
at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:224:29)
at compilePath (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:174:14)
at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:98:20)
at Object.<anonymous> (/usr/local/lib/node_modules/coffee-script/bin/coffee:7:41)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
The code I'm working with is:
https://pastebin.com/pVYwHpLc
(Don't worry, I changed the keys.)
I'm confused what and where the error is (front end is my thing). Any help is appreciated.
Sorry to bother.
Although a bit late answer, the error is on line 12:
"204.97.104.30",
You should delete the comma character (,) to have a valid JSON
I'm newbie of javascript and I want to open json file in node.js.
What's the problem of this "SyntaxError: Unexpected token"?
$ cat jsfile
{'key1':'val1', 'key2':'val2'}
obj = require("./jsfile");
jsfile:1
(function (exports, require, module, __filename, __dirname) {
{'key1':'val1', 'key2':'val2'}
^
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at repl:1:7
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
Your file isn't valid JSON.
JSON strings should be delimited by double quotes, not single quotes.
So your file should look like this:
{"key1":"val1", "key2":"val2"}
Also, JSON files should have a .json extension:
$ cat jsfile.json
{"key1":"val1", "key2":"val2"}
obj = require('./jsfile.json');
I have a string which I would like to turn into an object so I can pass it to mongoose
string = "{setting: {foo: false}}"
options = JSON.parse(string)
but this is giving me this error
SyntaxError: Unexpected token s
at Object.parse (native)
at Object.<anonymous> (/Users/home/blah/blah/blah.js:48:20)
at Object.<anonymous> (/Users/home/blah/blah/blah.js:54:4)
at Module._compile (module.js:449:26)
at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:83:25)
at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:177:29)
at fs.stat.notSources.(anonymous function) (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:152:18)
at fs.readFile (fs.js:176:14)
at Object.oncomplete (fs.js:297:15)
any idea what I'm doing wrong?
The JSON isnt right.
strthing ='{"setting": {"foo": false}}';
options = JSON.parse(strthing);
alert(options.setting.foo); ----> False.
http://jsfiddle.net/eaXjk/