1

A REST API sends me JSON back with URLs as fields.

How do I read these fields with Javascript? This doesn't work...

var json = JSON.parse(profile);

console.log(json["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality"]); 

Error:

SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at Strategy._verify (D:\Development\NodeJS\EID_TEST_NODEJS\server.js:33:21)
at validateCallback (D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\passport-saml\lib\passport-saml\strategy.js:61:14)
at D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\passport-saml\lib\passport-saml\saml.js:845:5
at _fulfilled (D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\q\q.js:854:54)
at self.promiseDispatch.done (D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\q\q.js:883:30)
at Promise.promise.promiseDispatch (D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\q\q.js:816:13)
at D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\q\q.js:624:44
at runSingle (D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\q\q.js:137:13)
at flush (D:\Development\NodeJS\EID_TEST_NODEJS\node_modules\q\q.js:125:13)

JSON send back from the api:

{
"issuer":"www.econtract.be",
"nameID":"00081007501",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality":"Hasselt",
"be:fedict:eid:idp:card-validity:end":"2023-03-03T00:00:00Z",
"be:fedict:eid:idp:card-number":"592548451825",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier":"00081007501",
"be:fedict:eid:idp:nationality":"Belg",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth":"2000-08-10T00:00:00Z",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender":"1",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalcode":"3511",  
etc....
}

Problem solved! Thanks @Mike

Change this code:

var json = JSON.parse(profile);
console.log(json["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality"]); 

To this code:

console.log(profile["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality"]);

Thanks for the help!

Michiel
  • 1,713
  • 3
  • 16
  • 34
  • Try posting your JSON in a site like https://jsonlint.com/. That will ensure that you are indeed receiving valid JSON. – William Fleming Mar 04 '18 at 15:42
  • You've got other problems besides that. Do a search on that error, and you'll find you're most likely parsing the JSON twice. Remember that JSON is a string format; check what `profile` is in a debugger and you'll likely find it's an object. – Heretic Monkey Mar 04 '18 at 15:44
  • 1
    Possible duplicate of [I keep getting "Uncaught SyntaxError: Unexpected token o"](https://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o) – Heretic Monkey Mar 04 '18 at 15:44
  • Your json is already an object. You try to parse an object not a json string. – Oktay Mar 04 '18 at 15:45
  • It's valid json, I've tested it; – Michiel Mar 04 '18 at 15:47
  • @Michiel So that narrows down that. I think Mike may be right, calling parse on an already parsed object will throw that error. If you remove the parse line, and access it on the profile, do you still see that error? – William Fleming Mar 04 '18 at 15:49
  • 1
    What is `typeof profile`? – str Mar 04 '18 at 15:50
  • It should ask you to accept the duplicate. Do that, and that'll be thanks enough :). – Heretic Monkey Mar 04 '18 at 15:57

1 Answers1

0

Observation :

You are trying to parse JSON Object. No need to parse JSON object again as it is already parsed while sending back from the API.

enter image description here

Working Demo

var profile = {
"issuer":"www.econtract.be",
"nameID":"00081007501",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality":"Hasselt",
"be:fedict:eid:idp:card-validity:end":"2023-03-03T00:00:00Z",
"be:fedict:eid:idp:card-number":"592548451825",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier":"00081007501",
"be:fedict:eid:idp:nationality":"Belg",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth":"2000-08-10T00:00:00Z",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender":"1",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalcode":"3511"
};

console.log(profile["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality"]); 
Debug Diva
  • 26,058
  • 13
  • 70
  • 123