0

please I have a json in this format below, I want to be able to access it values. Example accessing blue

 {
      "blue": [
        {
          "name": "Jim",
          "color": "blue",
          "age": 22
        },
        {
          "name": "Sam",
          "color": "blue",
          "age": 33
        }
      ],
          "green": [
            {
              "name": "Eddie",
              "color": "green",
              "age": 77
           }
      ]
     }

How can I access "blue"

  • Can you give some sample codes for more information? – spikef May 11 '23 at 11:24
  • 2 ways that I know. Let's say this is your json data => const data = { "blue": [ { "name": "Jim", "color": "blue", "age": 22 }, { "name": "Sam", "color": "blue", "age": 33 } ], "green": [ { "name": "Eddie", "color": "green", "age": 77 } ] }; 1. Dot notation `const blue= data.blue;` 2. Square bracket notation `const blue= data["blue"];` – Rationalist May 11 '23 at 11:25

1 Answers1

0

You can use this:-

let data = JSON.parse(jsonData);
console.log(data.blue);

where jsonData is the data in the JSON format.

Developer
  • 1,297
  • 1
  • 4
  • 15
  • This is such a general question, you don't need to answer it again. – Wimanicesir May 11 '23 at 11:25
  • @Wimanicesir Please. I know its general but who will define the general?? The general for you or me doesn't mean it is general for all. If someone is asking we should help, instead of demotivating him. – Developer May 11 '23 at 11:26
  • If you can find in a duplicate question better explained then here. General knowledge can be explained, but not over and over agian. Compare you answer with this: https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json/11922384#11922384. Now tell me why your answer is better than linking a already existing question. – Wimanicesir May 11 '23 at 11:30
  • Also i'm not demotivating him. I'm providing him with a better and more complete answer. You just give two lines with hardly any explanation. – Wimanicesir May 11 '23 at 11:32
  • Yes I will practice it for the middle/senior players in the game. If someone is beginner or requesting quick answer, I will try to provide answer as fast as I can. – Developer May 11 '23 at 11:32
  • I can provide explanation also. But I know there are some people who are just here to close the question etc. And many times, I follow even after the answer, if he will ask explanation, I will provide more. Don't worry. And think he can search on google or stackoverflow for this json question, but he may didn't find the answer. Then he ask here. And I help him. Easy pezzy. And yes I will link related questions, but if he need quick answer, I will provide answer quick instead of searching for related questions – Developer May 11 '23 at 11:35
  • Good you want to help fast. I am just trying to teach you community guide lines. And a first reflex should be looking up if there is already an answer provided on SO. If not, then you can provide an answer :) – Wimanicesir May 11 '23 at 11:35
  • You have your own interpretation of this site, but this is site is a community. And I'm trying to make you clear, you are not doing any good for this site :) – Wimanicesir May 11 '23 at 11:37
  • @Wimanicesir Oky. Great. Hope you understand my point. And I understand your point also. Try my best to follow the SO guidelines. Can you provide me the link of guideline regarding this duplicate posts? – Developer May 11 '23 at 11:41
  • Ofcourse this is a general explanation: https://stackoverflow.com/help/duplicates. And here is a post why you shouldn't answer these: https://meta.stackexchange.com/questions/10841/how-does-duplicate-closing-work-when-is-a-question-a-duplicate-and-how-should – Wimanicesir May 11 '23 at 11:44