Json parsing in Connect's bodyParser - json

I am sending this json to express app and getting parsing error:
{
"first_name" : "User",
"last_name" : "one",
"country" : "IN",
"dob" : "1980-04-11",
"email": "userone#gmail.com",
"password": "abcd12345",
"username": "usernameone"
}
The error is:
SyntaxError: Unexpected string
at Object.parse (native)
at IncomingMessage.<anonymous> (/Users/saransh2012/Developer/vypics/node_modules/express/node_modules/connect/lib/middleware/json.js:76:27)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
I think the error is in the date thing, what am I doing wrong?? Please help.

Related

Google Safe Browsing invalid JSON payload error with curl using a file

When I run this command (on macOS zsh):
curl -X POST -v "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=[MY-API-KEY]" -H "Content-Type: application/json" -d #/path/to/my/test.json --http1.1
I get this error message:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \" #/path/to/my/test.json\": Cannot bind query parameter. Field ' #/path/to/my/test' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \" #/path/to/my/test.json\": Cannot bind query parameter. Field ' #/path/to/my/test' could not be found in request message."
}
]
}
]
}
}
What's wrong with my command?
My test.json file contents is exactly as instructed here: https://developers.google.com/safe-browsing/v4/lookup-api

JSON.parse error 785 unexpected token error

I am trying to parse JSON data in my Rails 5 application with JSON.parse. I keep getting this error:
"#<JSON::ParserError: 785: unexpected token at ': Hours,\n \"config\": {\n \"blocks\": <\n {\n \"type\": table,\n \"name\": Hours from Users,\n \"fields\": [\n \"customer\",\n \"project\",\n \"user\",\n \"task\",\n \"hour_type\",\n \"amount\"\n ]\n }\n >\n }>"
here is what it looks like if you print the message:
#<JSON::ParserError: 785: unexpected token at ': Hours,
"config": {
"blocks": <
{
"type": table,
"name": Hours from Users,
"fields": [
"customer",
"project",
"user",
"task",
"hour_type",
"amount"
]
}
>
}>
you forgot to quote certain values like table and Hours from Users, they need double-quotes " to be valid json.
(also not sure about the extra < and >, might be coming from the parser error)
Change from :json to :hybrid like:
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
as explained in this Rails issue:

SyntaxError: Unexpected token } curly brace in JSON

Ok, for some reason, I tested something just to see if it would work and it gave me an error. I can't figure out what's wrong, it looks fine to me, I've compared and searched for an hour and nothing. I could be doing something really stupid but here.
This is bot.js
const botSettings = require("./botsettings.json");
console.log(botSettings.token);
console.log(botSettings.prefix);
This is package.json
{
"name": "ultibot",
"version": "0.0.1",
"description": "a bot for the discord server The Ritual",
"main": "bot.js",
"author": "Rituliant",
"license": "ISC",
"dependencies": {
"discord.js": "^11.3.0"
}
}
This is botsettings.json
{
"token": "thisisnormallyalongstringofrandomletters",
"prefix": "!",
}
The full error is this
module.js:665
throw err;
^
SyntaxError: C:\Users\quinb\DiscordBotJS\botsettings.json: Unexpected token } in JSON at position 98
at JSON.parse (<anonymous>)
at Object.Module._extensions..json (module.js:662:27)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\quinb\DiscordBotJS\bot.js:1:83)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
botsettings.json should be
{
"token": "thisisnormallyalongstringofrandomletters",
"prefix": "!"
}
i.e. no comma after the prefix value.
JSON, unlike modern JavaScript, does not allow trailing commas in its object notation. So the final } in botsettings.json is indeed unexpected, because of the comma before it:
{
"token": "thisisnormallyalongstringofrandomletters",
"prefix": "!",
^----- Here
}
That's only valid JSON if you remove that comma:
{
"token": "thisisnormallyalongstringofrandomletters",
"prefix": "!"
}

How do I load webpack resource in JSON

I have the following code...
[
{
"number": 0,
"color": "blue",
"content": require('./first-card.pug')
}
...
]
When I try to run Webpack I see...
ERROR in ./src/app/jg/cards/cards.json
Module build failed: SyntaxError: Unexpected token r
at Object.parse (native)
Do I need to escape or something?

Unexpected token { when parsing 'valid' json

I am trying to assign an array stored in json into an array in a controller when I press a button. I have run my json against several online validators and all of them claim my json is valid. However, when I go to parse it I get an unexpected token error.
I have double checked everything and I think everything closely mirrors the tutorial I am going through at https://docs.angularjs.org/tutorial/step_05 but it just doesn't want to work.
Relevant Controller Lines:
angular.module('myApp.controllers', [])
.controller('PlayerAdditionCtrl', function($scope, $http) {
$scope.players = [];
$scope.loadDefaults = function()
{
$http.get('defaults.json').success(function(array)
{
//assignment here
});
}
});
Complete json:
[
{
"name": "Mike",
"color": "teal"
},
{
"name": "Madre",
"color": "seagreen"
},
{
"name": "Anthony",
"color": "royalblue"
},
{
"name": "GI-Joe",
"color": "olivedrab"
}
]
Complete error text from chrome:
SyntaxError: Unexpected token {
at Object.parse (native)
at fromJson (http://localhost:8000/app/bower_components/angular/angular.js:1078:14)
at $HttpProvider.defaults.defaults.transformResponse (http://localhost:8000/app/bower_components/angular/angular.js:7317:18)
at http://localhost:8000/app/bower_components/angular/angular.js:7292:12
at Array.forEach (native)
at forEach (http://localhost:8000/app/bower_components/angular/angular.js:323:11)
at transformData (http://localhost:8000/app/bower_components/angular/angular.js:7291:3)
at transformResponse (http://localhost:8000/app/bower_components/angular/angular.js:7963:17)
at wrappedCallback (http://localhost:8000/app/bower_components/angular/angular.js:11319:81)
at http://localhost:8000/app/bower_components/angular/angular.js:11405:26 angular.js:9778
(anonymous function) angular.js:9778
(anonymous function) angular.js:7216
wrappedCallback angular.js:11322
(anonymous function) angular.js:11405
Scope.$eval angular.js:12412
Scope.$digest angular.js:12224
Scope.$apply angular.js:12516
done angular.js:8204
completeRequest angular.js:8412
xhr.onreadystatechange angular.js:8351
Check your json/and/or/pathtojson again your code works for me
http://plnkr.co/edit/AGdJ9SasmHn74RQe8xX2?p=preview