-1

I have the following JSON object that I receive from an API endpoint, how do I access description property of this object - "description" : "Dorian Market 1"?

let markets = { 
  "result":[
     {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 1",
      "description" : "Dorian Market 1",
      "minOrderValue" : "500",
      "notes" : ""
   } 
      ,
        {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 2",
      "description" : "Dorian Market 2 ",
      "minOrderValue" : "500",
      "notes" : ""
   } 
       ]
}

Is this the right way. markets.result[0].description, is this the right way?

Ivana
  • 842
  • 2
  • 15
  • 33
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Apr 17 '20 at 16:33
  • The `markets` variable refers to an object with a property called `result` (`markets.result`) which refers to an array. The information you want is in the first entry (`markets.result[0]`), which is an object, in the description property (`markets.results[0].description`). If you needed to search for it (e.g., in case it's not in the first entry), you'd probably want to use `find` on `markets.result`. – T.J. Crowder Apr 17 '20 at 16:34

1 Answers1

1

Yes, the right way to access the description property of the first element of the result array is using markets.result[0].description.

let markets = {
  "result":[
    {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 1",
      "description" : "Dorian Market 1",
      "minOrderValue" : "500",
      "notes" : ""
    },
    {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 2",
      "description" : "Dorian Market 2 ",
      "minOrderValue" : "500",
      "notes" : ""
    }
  ]
};

console.log(markets.result[0].description)
D. Pardal
  • 6,173
  • 1
  • 17
  • 37
  • 2
    We have [a canonical question](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) for this. Please vote to close rather than posting additional answers. You can always comment to help the OP apply the information from the other question's answers. – T.J. Crowder Apr 17 '20 at 16:33