最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Read property from a Json object in typescriptjavascript - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 2

You 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

发布评论

评论列表(0)

  1. 暂无评论