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.

5
  • Please learn the difference between JSON and the Object Literal Notation. And provide a minimal reproducible example. What are you trying to do with JSON.parse(JSON.stringify(jsonData));? Commented Dec 15, 2020 at 19:12
  • 2
    I have tested and it's working as expected .. Ham on Rye JS FIDDLE Commented Dec 15, 2020 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. Commented Dec 15, 2020 at 19:16
  • as @Zak said, the code you posted is working fine, I've also tested Commented Dec 15, 2020 at 19:21
  • It’s not an array of json objects within a string. it’s a json string containing an array of objects. Commented Dec 15, 2020 at 19:21

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this fixed it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.