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/
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.
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');
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.
I am trying to get a Lambda function to access an API and return the JSON.
API
http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a
P.S This API ID is the demo one provided by OW.
Lambda Code
var jsonurl = "http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a";
var data = JSON.parse(jsonurl);
exports.handler = function(event, context) {
console.log(data);
context.done(null, data); // SUCCESS with message
};
Error
{
"errorMessage": "Unexpected token h",
"errorType": "SyntaxError",
"stackTrace": [
"Object.parse (native)",
"Object.<anonymous> (/var/task/index.js:3:17)",
"Module._compile (module.js:456:26)",
"Object.Module._extensions..js (module.js:474:10)",
"Module.load (module.js:356:32)",
"Function.Module._load (module.js:312:12)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
Log output
START RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45 Version: $LATEST
Syntax error in module 'index': SyntaxError
at Object.parse (native)
at Object.<anonymous> (/var/task/index.js:3:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
END RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45
REPORT RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45 Duration: 173.76 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 9 MB
Can anyone see the problem?
What I would like is for lambda to get the json and return it, so anyone looking at my API url will see the results from the Open Weather API
You are passing a URL to JSON.parse(), not a JSON string. First you need to go get the JSON data from the URL using something like http.get(). Perhaps check out the answers to this similar question: Parsing JSON data from a URL