SReject / JSON-For-Mirc

JSON parser for mIRC
20 stars 6 forks source link

How to read json file #48

Closed preginald closed 4 years ago

preginald commented 4 years ago

I have a json file at c:\temp\nowplaying\nowplaying.json

It contains the following:

{ "nowplaying": { "playing": 1, "paused": 0, "albumartist": "Awesome Artist", "album": "soundcloud.com/thisGuy", "artist": "Awesome Artist", "title": "More please", "tracknumber": 0, "length": 393, "elapsed": 0}

How can I read that json file?

This is what I have so far

on *:TEXT:!song:#: {
  JSONOpen -u wr c:\temp\nowplaying\nowplaying.json

  ;; get the first item* in the wr json's root object then get the user specifiec catagory
  ;; *: In json indexes start at 0 not 1
  var %ref = $JSON(wr, 0)

  if ($JSONError) {
    ;; handle error
    var %album = $JSONError
  }
  elseif (!%ref) {
    ;; catagory doesn't exist
    var %album = don't exist
  }
  else {
    ;; get player and time values
    var %album = $JSON(%ref, album)
    var %artist = $json(%ref, artist)
    var %title = $json(%ref, title)

    ;; DO STUFF HERE WITH %artist, %title AND %album
  }

  msg $chan hello %album
}
westor7 commented 4 years ago

Try use that code:

ON *:TEXT:!song:#: {
  var %v = now_playing_ $+ $ctime $+ $ticks

  JSONOpen -df %v c:\temp\nowplaying\nowplaying.json
  if ($JSONError) { msg $chan Error: There was an reading error! - Error Details: $v1 | return }

  var %album = $json(%v,nowplaying,album).value
  var %artist = $json(%v,nowplaying,artist).value
  var %title = $json(%v,nowplaying,title).value
  var %paused = $json(%v,nowplaying,paused).value
  var %length = $json(%v,nowplaying,length).value

  msg $chan Currently $iif(%paused,Paused,Playing) $+ : $iif(%title,$v1,N/A) - Artist: $iif(%artist,$v1,N/A) - Album: $iif(%album,$v1,N/A) - Length: $iif(%length,$duration($v1),N/A)
}
preginald commented 4 years ago

Thanks for your reply @westor7

I'm starting to understand how your code works. I'm currently getting the following error:

Error: There was an reading error! - Error Details: INVALID_JSON

I think it could be because of the way the nowplaying.json file is formatted. When I open it in vscode the entire json data appears on a single line rather than multi line. Please see attached file nowplaying.zip

preginald commented 4 years ago

OK I removed the white spaces in the json file and I'm still getting the _INVALIDJSON error

westor7 commented 4 years ago

As i tested the file in http://jsonviewer.stack.hu/ it was not error , now i don't know why that error appear , probably @SReject has a better solution about it.

Also https://codebeautify.org/jsonvalidator said it is valid too..

I am curious too about that , so i re-opened just in case it is an issue..

preginald commented 4 years ago

@westor7 ok I think the error is happening because the json file is not formatted correctly. When I used jsonviewer.stack.hu to format the data and save that into a test.json file then your code works beautifully.

So now it's a matter of formatting the string into json format.

preginald commented 4 years ago

@westor7 ok I found the cause.

I noticed that the now_playing.json file is being encoded as UTF-8 with BOM. When I copied the exact json content, ie the entire string in one line, and saved it to a new temp.json file with UTF-8 encoding and ran your script then it worked as expected.

According to this post

So now the question is, how to convert to UTF-8 without BOM in the mIRC script?

preginald commented 4 years ago

I fixed the issue and your code works. I needed to select the encoding from the plugin to UTF-8 instead of UTF-8 with header.

image

But @SReject might suggest a way to remove the header from file in case someone else needs the functionality.

Thanks for all your help.