Weird JSON

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Weird JSON

Post by collectordave »

I am extracting some JSON data but keep failing with one nameused in the JSON string.

I have used a structure as below:-

Structure ImageURL
#text.s
size.s
EndStructure

The line #text.s gives me a compile error so changed to

Structure ImageURL
text.s
size.s
EndStructure

No #


The JSON uses #text as the key?

{
"#text": "https://lastfm-img2.akamaized.net/i/u/3 ... db1638.png",
"size" : "small"
},

I extract everything except the #text part.

I thought it might be because i used just 'text'

Can I get over this somehow?

Regards

Cd
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Weird JSON

Post by collectordave »

Of course just as i ask the question my feeble attempts give results.

Code: Select all

MyJSONString = ComposeJSON(Json)

MyJSONString = ReplaceString(MyJSONString,"#","")

Json = ParseJSON(#PB_Any,MyJSONString)

Compose to a string get rid of the '#' then parse again.

Works now!

Hope this helps someone else.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Weird JSON

Post by Derren »

Well, if this is not to be considered a bug, it will surely lead to bugs when you rely on this function.
"#text" is valid JSON. Anything between " and " that can be considered a string/character is valid JSON. Even Unicode Emojis.

So if the PB function can't handle it, it needs to be at least addressed in the help files.

Better would be a function that maps the JSON keys to valid PB Structure fields.

Just as an example (there might be better ways to do this), something like this.

Code: Select all

Structure myJson
    text.s
    grin_emoji.s
EndStructure

Define keys.myJson
keys\text = "#text"
keys\grin_emoji = ""
Then again, why don't you use ExtractJSONMap()?
It works with all kinds of strings:

Code: Select all

a.s = "{?#text?: ?https://lastfm-img2.akamaized.net/i/u/34s/3b54885952161aaea4ce2965b2db1638.png?,?size? : ?small?}"
b.s = ReplaceString(a, "?", Chr(34))

Debug b

Debug ParseJSON(0, b)

NewMap myJson.s()

ExtractJSONMap(JSONValue(0), myJson())   

Debug myJson("#text")
Debug myJson("size")

edit: But the board does not work with Unicode? WTH?
This was the Code I was trying to paste..
https://pastebin.com/e3Dfs0Na
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Weird JSON

Post by collectordave »

Hi

Not a bug. It is the leading character that raises an error at compile time.

This is also linked to topic viewtopic.php?f=13&t=73450

I am now using a recursive procedure.

Got over the first one with:-

MyJSONString = ReplaceString(MyJSONString,"#","")

and then parseing that string.

but found another.

Here is my code

Code: Select all

Structure Artist
  name.s
  url.s
  mbid.s
EndStructure

Structure TrackNum
  rank.s
EndStructure

Structure Track
  artist.Artist
  attr.TrackNum    ; @attr ?
  name.s
  url.s
  duration.s
EndStructure

Global Dim Tracks.Track(0)

Procedure GetTracks (v.i)
   Protected.i i, last, jn
   
   Static TrackFound.i
   
   Select JSONType(v)
      
      Case #PB_JSON_Array
        
        If TrackFound = #True
          
          ExtractJSONArray(v,Tracks())
          
          TrackFound = #False
  
        EndIf

      Case #PB_JSON_Object

         If ExamineJSONMembers(v)
            While NextJSONMember(v)
              
              If JSONMemberKey(v) = "track"
                
                TrackFound = #True

              EndIf

               jn = JSONMemberValue(v)
               GetTracks(jn)
     
            Wend
         EndIf
   EndSelect
EndProcedure

MyJSONString.s

jn = LoadJSON(#PB_Any,"C:\Temp\Cher tracks.json")

MyJSONString = ComposeJSON(jn)

MyJSONString = ReplaceString(MyJSONString,"#","")

MyJSONString = ReplaceString(MyJSONString,"@","")

json = ParseJSON(#PB_Any,MyJSONString)

GetTracks(JSONValue(json))

For i = 0 To ArraySize(Tracks())
  
  Debug "Track Number " + Tracks(i)\attr\rank
  Debug "Title " + Tracks(i)\name  
  Debug "Artist " + Tracks(i)\Artist\name
  Debug "URL " + Tracks(i)\url
  
  Debug ""
  
Next
As you can see I have removed all the '@' symbols and it works.

Is there a better way?

Here is the json string:-

Code: Select all

{"album":{"name":"Believe","artist":"Cher","mbid":"63b3a8ca-26f2-4e2b-b867-647a6ec2bebd","url":"https://www.last.fm/music/Cher/Believe","image":[{"#text":"https://lastfm-img2.akamaized.net/i/u/34s/3b54885952161aaea4ce2965b2db1638.png","size":"small"},{"#text":"https://lastfm-img2.akamaized.net/i/u/64s/3b54885952161aaea4ce2965b2db1638.png","size":"medium"},{"#text":"https://lastfm-img2.akamaized.net/i/u/174s/3b54885952161aaea4ce2965b2db1638.png","size":"large"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/3b54885952161aaea4ce2965b2db1638.png","size":"extralarge"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/3b54885952161aaea4ce2965b2db1638.png","size":"mega"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/3b54885952161aaea4ce2965b2db1638.png","size":""}],"listeners":"389654","playcount":"2494430","tracks":{"track":[{"name":"Believe","url":"https://www.last.fm/music/Cher/_/Believe","duration":"238","@attr":{"rank":"1"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"The Power","url":"https://www.last.fm/music/Cher/_/The+Power","duration":"236","@attr":{"rank":"2"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Runaway","url":"https://www.last.fm/music/Cher/_/Runaway","duration":"286","@attr":{"rank":"3"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"All or Nothing","url":"https://www.last.fm/music/Cher/_/All+or+Nothing","duration":"238","@attr":{"rank":"4"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Strong Enough","url":"https://www.last.fm/music/Cher/_/Strong+Enough","duration":"213","@attr":{"rank":"5"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Dov'è L'amore","url":"https://www.last.fm/music/Cher/_/Dov%27%C3%A8+L%27amore","duration":"258","@attr":{"rank":"6"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Takin' Back My Heart","url":"https://www.last.fm/music/Cher/_/Takin%27+Back+My+Heart","duration":"272","@attr":{"rank":"7"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Taxi Taxi","url":"https://www.last.fm/music/Cher/_/Taxi+Taxi","duration":"304","@attr":{"rank":"8"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Love Is the Groove","url":"https://www.last.fm/music/Cher/_/Love+Is+the+Groove","duration":"271","@attr":{"rank":"9"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"We All Sleep Alone","url":"https://www.last.fm/music/Cher/_/We+All+Sleep+Alone","duration":"233","@attr":{"rank":"10"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}}]},"tags":{"tag":[{"name":"pop","url":"https://www.last.fm/tag/pop"},{"name":"90s","url":"https://www.last.fm/tag/90s"},{"name":"dance","url":"https://www.last.fm/tag/dance"},{"name":"cher","url":"https://www.last.fm/tag/cher"},{"name":"albums I own","url":"https://www.last.fm/tag/albums+I+own"}]},"wiki":{"published":"27 Jul 2008, 15:55","summary":"Believe is the twenty-third studio album by American  singer-actress Cher, released on November 10, 1998 by Warner Bros. Records. The RIAA certified it Quadruple Platinum on December 23, 1999, recognizing four million shipments in the United States; Worldwide, the album has sold more than 20 million copies, making it the biggest-selling album of her career. In 1999 the album received three Grammy Awards nominations including \"Record of the Year\", \"Best Pop Album\" and winning \"Best Dance Recording\" for the single \"Believe\". <a href=\"http://www.last.fm/music/Cher/Believe\">Read more on Last.fm</a>.","content":"Believe is the twenty-third studio album by American  singer-actress Cher, released on November 10, 1998 by Warner Bros. Records. The RIAA certified it Quadruple Platinum on December 23, 1999, recognizing four million shipments in the United States; Worldwide, the album has sold more than 20 million copies, making it the biggest-selling album of her career. In 1999 the album received three Grammy Awards nominations including \"Record of the Year\", \"Best Pop Album\" and winning \"Best Dance Recording\" for the single \"Believe\".\n\nIt was released by Warner Bros. Records at the end of 1998. The album was executive produced by Rob Dickens. Upon its debut, critical reception was generally positive. Believe became Cher's most commercially-successful release, reached number one and Top 10 all over the world. In the United States, the album was released on November 10, 1998, and reached number four on the Billboard 200 chart, where it was certified four times platinum.\n\nThe album featured a change in Cher's music; in addition, Believe presented a vocally stronger Cher and a massive use of vocoder and Auto-Tune. In 1999, the album received 3 Grammy Awards nominations for \"Record of the Year\", \"Best Pop Album\" and winning \"Best Dance Recording\". Throughout 1999 and into 2000 Cher was nominated and winning many awards for the album including a Billboard Music Award for \"Female Vocalist of the Year\", Lifelong Contribution Awards and a Star on the Walk of Fame shared with former Sonny Bono. The boost in Cher's popularity led to a very successful Do You Believe? Tour.\n\nThe album was dedicated to Sonny Bono, Cher's former husband who died earlier that year from a skiing accident.\n\nCher also recorded a cover version of \"Love Is in the Air\" during early sessions for this album. Although never officially released, the song has leaked on file sharing networks.\n\nSingles\n\n\n\"Believe\"\n\"Strong Enough\"\n\"All or Nothing\"\n\"Dov'è L'Amore\" <a href=\"http://www.last.fm/music/Cher/Believe\">Read more on Last.fm</a>. User-contributed text is available under the Creative Commons By-SA License; additional terms may apply."}}}
Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply