Home > database >  XQuery How can I get the name of the Artist from the JSON
XQuery How can I get the name of the Artist from the JSON

Time:12-02

I have the following JSON file

{
"href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX2RxBh64BHjQ/tracks?offset=0&limit=100&additional_types=track",
"items": [
    {
        "added_at": "2021-11-29T23:29:54Z",
        "added_by": {
            "external_urls": {
                "spotify": "https://open.spotify.com/user/"
            },
            "href": "https://api.spotify.com/v1/users/",
            "id": "",
            "type": "user",
            "uri": "spotify:user:"
        },
        "is_local": false,
        "primary_color": null,
        "track": {
            "album": {
                "album_type": "single",
                "artists": [
                    {
                        "external_urls": {
                            "spotify": "https://open.spotify.com/artist/0Njy6yR9LykNKYg9yE23QN"
                        },
                        "href": "https://api.spotify.com/v1/artists/0Njy6yR9LykNKYg9yE23QN",
                        "id": "0Njy6yR9LykNKYg9yE23QN",
                        "name": "Nardo Wick",
                        "type": "artist",
                        "uri": "spotify:artist:0Njy6yR9LykNKYg9yE23QN"
                    }
                ],
                "available_markets": [
                    "AD",
                    "AE",
                    "AG",
                    "AL",
                    "AM",
                ],
                "external_urls": {
                    "spotify": "https://open.spotify.com/album/6SEeNB2xGW1kmysKSvWYqC"
                },
                "href": "https://api.spotify.com/v1/albums/6SEeNB2xGW1kmysKSvWYqC",
                "id": "6SEeNB2xGW1kmysKSvWYqC",
                ],
                "name": "Me or Sum (feat. Future & Lil Baby)",
            }

Im tying to get the name of the Artist, using Xquery, which is "Nardo Wick", but somethingis wrong. Here is my code:

  xquery version "3.1";
let $hou := fn:json-doc("spoti.json")?artists?*
return
   <Playlist>   
      <song>
      {
         for $item in fn:distinct-values($hou?name)
         return <name>{$item}</name>
      }
      </song>
   </Playlist>

So, my question is, how can I get the name of the artist from this JSON or anything else which is inside the "artists".

CodePudding user response:

Your JSON snippet is not complete but some more realistic selection (attempting to select down into the presented JSON) would be ?items?1?track?album?artists?1?name. (Untested)

CodePudding user response:

You could consider using

let $hou := (fn:json-doc("spoti.json") => map:find("artists"))?*

Though this is a little fragile as it assumes the key "artists" doesn't appear anywhere else.

  • Related