0

I have a problem getting access to the variables in a JSON-File. I get the file as a response from a REST-API. Here you can see the JSON-File:

{
"_embedded": {
    "events": [
        {
            "name": "Josh.",
            "type": "event",
            "id": "Z698xZC2Z17CebP",
            "test": false,
            "url": "https://www.ticketmaster.de/event/josh-tickets/382625?language=en-us",
            "locale": "en-us"

I want to access the value (Josh) of the "name" variable in Javascript in line number 5 and store it into a new variable to use it later.

This ist how I try to do it:

var = data._embedded.events[0].name

But I can't get access to the value attached to "name".

I have read the answers to similar questions here, but there was no solution for me.

I have only read, that it does not work because of the wrong JSON-structure. In other threads, they say there has to be a "{" instead of a "[" in line 3. But I don't know how to change it in javascript directly. I tried it with the JSON.stringify()-method but it didn't work out.

Can somebody help me, please? Is there any way to get access?

I hope you can understand my problem.

Thank you very much!

Sakhund
  • 228
  • 1
  • 5
  • 32
LDP97
  • 1
  • 1
  • [It depends on how you're accessing the JSON.](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) (But also `var = data._embedded.events[0].name` will give you a error). – Andy Nov 10 '21 at 00:29
  • Hello Andy, sorry I am a Beginner and I do not understand your hint/ Link. Which way for accessing the JSON would you prefer? And why will var = data._embedded.events[0].name give me a error? – LDP97 Nov 10 '21 at 00:34
  • Hey, no problem. We all start at the beginning. So start debugging. Open your devtools (F12), go to the console tab and type in `var = 'something'`. You'll see the error. It's because you're not assigning data to a variable. You may have meant something like `var name = data._embedded.events[0].name`. – Andy Nov 10 '21 at 00:48
  • The other thing is a little more tricky to diagnose. Can you add the code that you're using to get the JSON to your question as a [mcve]? – Andy Nov 10 '21 at 00:50
  • @LDP97 you json is invalid , can you post the real one pls? – Serge Nov 10 '21 at 01:07
  • @LDP97 is it the actual result from the Rest-API, if so, you may contact the REST-API creator for support. – The KNVB Nov 10 '21 at 01:31
  • you can try use online evaluator https://jsonpath.com/ , from your JSON structure use this JSONPath Syntax `$._embedded.events[0].name` – hendra Nov 10 '21 at 02:25
  • Thank you guys for all your help, even if I didn't post the whole JSON-File. I got it and I could fix the problem. With jsonpath.com and the JSON path syntax it worked! Thank you all very much! – LDP97 Nov 10 '21 at 07:10

1 Answers1

0

It seems that your json structure has some errors. The json structure similar to the following is legal. Of course, it is no problem to use a legal structure to exec data._embedded.events[0].name.

const data = {
  "_embedded": {
      "events": [
          {
              "name": "Josh.",
              "type": "event",
              "id": "Z698xZC2Z17CebP",
              "test": false,
              "url": "https://www.ticketmaster.de/event/josh-tickets/382625?language=en-us",
              "locale": "en-us"
          },
          {
              "name": "Josh.",
              "type": "event",
              "id": "Z698xZC2Z17CebP",
              "test": false,
              "url": "https://www.ticketmaster.de/event/josh-tickets/382625?language=en-us",
              "locale": "en-us"
          },
          //...
      ]
   }
}

console.log(data._embedded.events[0].name)
  • Thanks for your answer! I had a problem with the structure and the JSON-Path-Syntax. Now it works! – LDP97 Nov 10 '21 at 07:11