I am working with an auth token with I receive from a third-party API. I have given a sample of the decoded token below,
{
"nbf": 1564128888,
"exp": 1564132488,
"iss": ":5002",
"aud": ":5002/resources",
"": "[email protected]",
"": "Micky Mouse",
"amr": ["custom"]
}
I am struggling to read the "name" claim in javascript. How can I read that property in javascript or typescript?
I am working with an auth token with I receive from a third-party API. I have given a sample of the decoded token below,
{
"nbf": 1564128888,
"exp": 1564132488,
"iss": "http://example.:5002",
"aud": "http://example.:5002/resources",
"http://schemas.xmlsoap/ws/2005/05/identity/claims/emailaddress": "[email protected]",
"http://schemas.xmlsoap/ws/2005/05/identity/claims/name": "Micky Mouse",
"amr": ["custom"]
}
I am struggling to read the "name" claim in javascript. How can I read that property in javascript or typescript?
Share Improve this question asked Jul 28, 2019 at 15:37 JoshuaJoshua 2,2958 gold badges42 silver badges57 bronze badges 5-
Could you clarify what you're trying to read? Is it that you need to find any keys that end in
/claims/name
and do something with their value? – GenericUser Commented Jul 28, 2019 at 15:41 - Is the name key dynamic ? – Asaf Aviv Commented Jul 28, 2019 at 15:44
- @AsafAviv what do you mean by that? – Joshua Commented Jul 28, 2019 at 15:45
- @GenericUser yes I am. Trying to read the name and do something with it. – Joshua Commented Jul 28, 2019 at 15:45
- 1 nvm check out Philip's answer – Asaf Aviv Commented Jul 28, 2019 at 15:47
3 Answers
Reset to default 2You can access plex property names like this:
const name = token["http://schemas.xmlsoap/ws/2005/05/identity/claims/name"]
You could also abstract this away for reusability (like the ClaimTypes
in C#)
const ClaimTypes = {
name: "http://schemas.xmlsoap/ws/2005/05/identity/claims/name",
// other relevant claims
};
const name = token[ClaimTypes.name];
You will get your data in form of a string, convert it to json
let jsonData = '{"nbf": 1564128888,"exp": 1564132488,"iss": "http://example.:5002","aud": "http://example.:5002/resources","http://schemas.xmlsoap/ws/2005/05/identity/claims/emailaddress": "[email protected]","http://schemas.xmlsoap/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}'
let parsedJSON = JSON.parse(jsonData)
console.log(parsedJSON["http://schemas.xmlsoap/ws/2005/05/identity/claims/name"]) // Micky Mouse
console.log(parsedJSON["nbf"]) // 1564128888
console.log(parsedJSON["http://schemas.xmlsoap/ws/2005/05/identity/claims/emailaddress"]) // [email protected]
And then read it like parsedJSON["your key"]
. Things on the left sides are property names or keys. You can retrive them by
let jsonData = '{"nbf": 1564128888,"exp": 1564132488,"iss": "http://example.:5002","aud": "http://example.:5002/resources","http://schemas.xmlsoap/ws/2005/05/identity/claims/emailaddress": "[email protected]","http://schemas.xmlsoap/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}'
let parsedJSON = JSON.parse(jsonData)
console.log(Object.keys(parsedJSON))
JSON.parse(yourData) - convert JSON to JS JSON.stringify(yourData) - from JS to JSON
so after JSON.parse you will get JS object and able to get yourData.name
Here you can read: MDN: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
you can try for example: https://jsonformatter/json-parser