0

I have an array of json object within a string:

[
  {
    "title": "Ham on Rye",
    "alternativeTitles": [],
    "secondaryYearSourceId": 0,
    "sortTitle": "ham on rye",
    "sizeOnDisk": 0,
    "status": "released",
    "overview": "A bizarre rite of passage at the local deli determines the fate of a generation of teenagers, leading some to escape their suburban town and dooming others to remain…",
    "inCinemas": "2019-08-10T00:00:00Z",
    "images": [
      {
        "coverType": "poster",
        "url": "http://image.tmdb.org/t/p/original/px7iCT1SgsOOSAXaqHwP50o0jAI.jpg"
      }
    ],
    "downloaded": false,
    "remotePoster": "http://image.tmdb.org/t/p/original/px7iCT1SgsOOSAXaqHwP50o0jAI.jpg",
    "year": 2019,
    "hasFile": false,
    "profileId": 0,
    "pathState": "dynamic",
    "monitored": false,
    "minimumAvailability": "tba",
    "isAvailable": true,
    "folderName": "",
    "runtime": 0,
    "tmdbId": 527176,
    "titleSlug": "ham-on-rye-527176",
    "genres": [],
    "tags": [],
    "added": "0001-01-01T00:00:00Z",
    "ratings": {
      "votes": 5,
      "value": 6.9
    },
    "qualityProfileId": 0
  }
]

When I try to parse my array of json objects, it always returns a string, so when I try to access it by:

let data = JSON.parse(JSON.stringify(jsonData));

console.log(data[0].title)

it returns a specific character like "[", as if it was a string.

jlhs
  • 103
  • 7
  • Please learn [the difference between JSON and the Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). And provide a [mcve]. What are you trying to do with `JSON.parse(JSON.stringify(jsonData));`? – str Dec 15 '20 at 19:12
  • 2
    I have tested and it's working as expected .. `Ham on Rye` [JS FIDDLE](https://jsfiddle.net/g0ecdo36/) – Zak Dec 15 '20 at 19:13
  • @str I'm sorry, I'll edit my question,as it's a little misleading. I'm getting this json data from a webserver. – jlhs Dec 15 '20 at 19:16
  • as @Zak said, the code you posted is working fine, I've also tested – cape_bsas Dec 15 '20 at 19:21
  • It’s not an array of json objects within a string. it’s a json string containing an array of objects. – James Dec 15 '20 at 19:21

1 Answers1

1

Te problem is that you are stringifying something that is already a JSON string.
Change this:

let data = JSON.parse(JSON.stringify(jsonData));

to this:

let data = JSON.parse(jsonData);
cape_bsas
  • 638
  • 5
  • 13