unicorn_surprise
unicorn_surprise

Reputation: 1109

Javascript Map not Escaping properly

for some reason I can't seem to return an array of properly escaped JSON below. My console gives a long list of incorrectly formatted array. Just wondering how to properly escape?enter image description here

const songsLdJson = songs.map((song: any) => {
    return `{
        "@type": "MusicRecording",
        "byArtist": "${song.song?.artist.name}",
        "name": "${song.song?.name}",
        "url": "sweet-home-alabama",
        "interactionStatistic": {
            "@type": "InteractionCounter",
            "interactionType": "https://schema.org/ListenAction",
            "userInteractionCount": "${song.played_count}"
        },
        "thumbnailUrl": "${song.song?.spotifyImg300}",
        "audio": {
          "@type": "AudioObject",
          "contentUrl": "${song.song?.preview_url}",
          "description": "${song.sceneDescription}",
          "encodingFormat": "audio/mpeg",
        }
      }`
  })
  console.log(songsLdJson, "JSON")

Upvotes: 0

Views: 84

Answers (1)

Kinglish
Kinglish

Reputation: 23654

Do you really want to create that object as a string? Better to create as an object, then stringify if needed.

const songsLdJson = songs.map((song: any) => {
  return {
    "@type": "MusicRecording",
    "byArtist": `${song.song?.artist.name}`,
    "name": `${song.song?.name}`,
    "url": "sweet-home-alabama",
    "interactionStatistic": {
        "@type": "InteractionCounter",
        "interactionType": "https://schema.org/ListenAction",
        "userInteractionCount": `${song.played_count}`
    },
    "thumbnailUrl": `${song.song?.spotifyImg300}`,
    "audio": {
      "@type": "AudioObject",
      "contentUrl": `${song.song?.preview_url}`,
      "description": `${song.sceneDescription}`,
      "encodingFormat": "audio/mpeg",
    }
  }
})
console.log(songsLdJson, "JSON")
// if you want that then turned into a string
songsLdJson = JSON.stringify(songsLdJson);

Upvotes: 2

Related Questions