1

I want to get the value by the key of this json. The key is description and i want it in my console.log, i don't really get how to get this value, the filtering before worked fine.

{
    "id": "149",
    "description": "Try"
}

Thanks!

2 Answers 2

2

First You need to parse it using JSON.parse. Then using . or [] you can access. For Ex:-

var a = '{
    "id": "149",
    "description": "Try"
}'

b = JSON.parse(a)

b['description'] or b.description FOr more reference you can study here

Sign up to request clarification or add additional context in comments.

Comments

2

const dummy = {
  id : '1',
  description : 'Wow awesome'
};

console.log(dummy.description);

//-------------------------------------

const descData = dummy.description;

console.log(descData);

variableName['description'] or variableName.description

9 Comments

But when i make: const test = ['description']. console.log got -> undefinied. Thats why i open up this threat
My use-case looks like, i got a big json file about 5000 lines long. Since one hour I made filters to get this tiny snippet above and now I want to get in my const the value of the key "description" to fill up my table on my html template.
@muelleste , please check the snippet, i think that will clear your doubts
console.log('treeNodes:' + JSON.stringify(treeNodes)); console.log(treeNodes.description); treeNodes:"{\"id\":\"147\",\description\":\"Try\"}" test.component.ts:42:13 undefined . That's my result
try treeNodes.description OR var data = JSON.parse(treeNodes); console.log(data.description);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.