It is currently March 28th, 2024, 11:30 am

Json Parser Plugin

Share and get help with Plugins and Addons
User avatar
limitless
Posts: 76
Joined: January 8th, 2017, 2:31 am
Location: Charlotte, NC
Contact:

Re: Json Parser Plugin

Post by limitless »

This is so cool. Thanks!!! ;-)
ElMalditoBatman
Posts: 2
Joined: April 4th, 2023, 3:06 pm

Re: Json Parser Plugin

Post by ElMalditoBatman »

Hi Sorry to dig up such an old thread, but hoping someone might be able to assist on this error.

I am trying to pull the name field after filtering for a specific server name in a json file. json file data seems to be downloading just fine. plugin seems to load no issues. But I am getting a "Error reading JToken from JsonReader. Path '', line 0, position 0." error on the logs whenever I try to query up for the information I need.

Below is the code I am currently using along with log.
[Rainmeter]
Update=10000
DynamicWindowSize=1

[MeasureArkServerList]
Measure=WebParser
URL=http://arkdedicated.com/xbox/cache/unofficialserverlist.json
RegExp=(?siU)^(.*)$



[MeasureNumplayers]
Measure=Plugin
Plugin=JsonParser.dll
Source=[MeasureArkServerList]
Query="Name.['[US] Back Seat Gaming Official'].MapName"

[MapGenesisLabel]
Meter=String
X=5
Y=3R
W=300
H=15
FontSize=11
FontColor=255,225,181,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Genesis 1:

[MeterGenesisPlayers]
Meter=String
MeasureName=MeasureNumplayers
X=315
Y=0r
W=300
H=15
FontSize=11
FontColor=252,251,202,255
SolidColor=0,0,0,1
Padding=5,5,5,5
StringAlign=Right
AntiAlias=1
Log
DBUG (11:01:46.769) : Useragent="Rainmeter WebParser plugin"
NOTE (11:01:46.773) HelloWorld\HelloWorld.ini - [MeasureArkServerList]: Debug file: C:\WebParserDump.txt
DBUG (11:01:46.780) : Plugin loaded: C:\Program Files\Rainmeter\Plugins\JsonParser.dll
DBUG (11:01:46.784) HelloWorld\HelloWorld.ini - [MeasureMapName]: Log Config:
DBUG (11:01:46.789) HelloWorld\HelloWorld.ini - [MeasureMapName]: Log Config: Log NoMatch: True
DBUG (11:01:46.793) HelloWorld\HelloWorld.ini - [MeasureMapName]: Log Config: Log EmptySource: True
DBUG (11:01:46.797) HelloWorld\HelloWorld.ini - [MeasureMapName]: Locale Config:
DBUG (11:01:46.801) HelloWorld\HelloWorld.ini - [MeasureMapName]: Source:
DBUG (11:01:46.806) HelloWorld\HelloWorld.ini - [MeasureMapName]: Query: Name.['[US] Back Seat Gaming Official'].MapName
ERRO (11:01:46.810) HelloWorld\HelloWorld.ini - [MeasureMapName]: Error reading JToken from JsonReader. Path '', line 0, position 0.
DBUG (11:01:46.814) HelloWorld\HelloWorld.ini - [MeasureMapName]: Result:
DBUG (11:01:46.818) HelloWorld\HelloWorld.ini - [MeasureArkServerList]: Fetching: http://arkdedicated.com/xbox/cache/unofficialserverlist.json
User avatar
Yincognito
Rainmeter Sage
Posts: 7017
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Json Parser Plugin

Post by Yincognito »

ElMalditoBatman wrote: April 4th, 2023, 3:13 pm Hi Sorry to dig up such an old thread, but hoping someone might be able to assist on this error.

I am trying to pull the name field after filtering for a specific server name in a json file. json file data seems to be downloading just fine. plugin seems to load no issues. But I am getting a "Error reading JToken from JsonReader. Path '', line 0, position 0." error on the logs whenever I try to query up for the information I need.
There are several problems with your approach:
1) the JSON is huge (24000+ records, 34.5 MB saved, 532000+ lines after formatting), so its retrieval is far from instantaneous - hence the error since the Source option has an empty (e.g. invalid) value before the entire data is eventually received by Rainmeter
2) the value of the RegExp option in your WebParser measure needs to be (?siU)^(.*?)$ so that the ? after the * inverts the ungreedy matching set by the ?U flag and captures as many characters as possible (aka all)
3) the value of the Query option in your JsonParser measure is incorrect - I suggest you study the details about the syntax of such a query by checking out the NewtonSoft link from the first post in this thread, in order to understand the changes to it below
4) dynamic variables need to be enabled in the JsonParser measure, since it references the WebParser measure which changes over the course of the process - alternatively, I guess you could use Source=[&MeasureArkServerList] instead of enabling them

The code (I left some comments there for conveniency):

Code: Select all

[Rainmeter]
Update=10000
AccurateText=1
DynamicWindowSize=1

--- First test sample from https://www.newtonsoft.com/json/help/html/SelectToken.htm with new lines removed ---

; [MeasureTest]
; Measure=String
; String="{  'Stores': [    'Lambton Quay',    'Willis Street'  ],  'Manufacturers': [    {      'Name': 'Acme Co',      'Products': [        {          'Name': 'Anvil',          'Price': 50        }      ]    },    {      'Name': 'Contoso',      'Products': [        {          'Name': 'Elbow Grease',          'Price': 99.95        },        {          'Name': 'Headlight Fluid',          'Price': 4        }      ]    }  ]}"

; [MeasureManufacturerName]
; Measure=Plugin
; Plugin=JsonParser.dll
; Source=[MeasureTest]
; Query="Manufacturers[0].Name"
; DynamicVariables=1

--- Actual sample from http://arkdedicated.com/xbox/cache/unofficialserverlist.json ---

; REFERENCES:
; - SourcePage: http://arkdedicated.com/xbox/cache/unofficialserverlist.json
; - Formatting: https://webformatter.com/
; - Query Info: https://www.newtonsoft.com/json/help/html/SelectToken.htm

[MeasureArkServerList]
Measure=WebParser
URL=http://arkdedicated.com/xbox/cache/unofficialserverlist.json
RegExp=(?siU)^(.*?)$

[MeasureNumPlayers]
Measure=Plugin
Plugin=JsonParser.dll
Source=[MeasureArkServerList]
Query="$[?(@.Name == '[US] Back Seat Gaming Official')].MapName"
DynamicVariables=1

--- Meters ---

[MapGenesisLabel]
Meter=String
X=5
Y=3R
W=300
H=15
FontSize=11
FontColor=255,225,181,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Genesis 1:

[MeterGenesisPlayers]
Meter=String
MeasureName=MeasureNumPlayers
X=315
Y=0r
W=300
H=15
FontSize=11
FontColor=252,251,202,255
SolidColor=0,0,0,1
Padding=5,5,5,5
StringAlign=Right
AntiAlias=1
Preview:
JsonParser.jpg
You'll have to wait several seconds to get the result, because the JSON is that big. The error will still appear at the start since the parsed value is then empty, but you will get the result anyway. The error can be avoided by setting the JsonParser measure to be Disabled initiallly, then !Enable it from the FinishAction of the WebParser measure, but then I suspect your main goal was to get the result you wanted in the first place.
ElMalditoBatman
Posts: 2
Joined: April 4th, 2023, 3:06 pm

Re: Json Parser Plugin

Post by ElMalditoBatman »

Dude you are amazing!. Thank you! And yes, I figured it had something to do with the huge json file. This is my first time working with rainmeter, and I appreciate all the notes. I was having trouble finding others with a similar matter to learn off of.
User avatar
Yincognito
Rainmeter Sage
Posts: 7017
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Json Parser Plugin

Post by Yincognito »

ElMalditoBatman wrote: April 4th, 2023, 7:12 pm Dude you are amazing!. Thank you! And yes, I figured it had something to do with the huge json file. This is my first time working with rainmeter, and I appreciate all the notes. I was having trouble finding others with a similar matter to learn off of.
You're welcome - glad to help. Thanks for the appreciation, others here are too!
Take your time and enjoy working with Rainmeter, and good luck with the rest of the JSON querying process! ;-)
User avatar
Kurou
Posts: 40
Joined: January 24th, 2022, 2:54 pm
Location: World Wide Web
Contact:

Re: Json Parser Plugin

Post by Kurou »

Hi, im working on my suite which uses your JsonParser plugin to check for updates. I have one major problem with this plugin, it works how it should but every time the page in my panel loads this error is printed into rainmeter logs.

Error:

Code: Select all

Error reading JToken from JsonReader. Path '', line 0, position 0. (ashuramaru\panel\panel.ini - [Measure_Version_Parse])
Error reading JToken from JsonReader. Path '', line 0, position 0. (ashuramaru\panel\panel.ini - [Measure_Version_Parse])
My version.json:

Code: Select all

{
    "Ashuramaru_Bundle": [
        {
            "version": "1.1.1"
        }
    ]
}
My code for update:

Code: Select all

[Measure_Version]
Measure=Webparser
Url=https://raw.githubusercontent.com/Kurou-kun/Ashuramaru-Bundle/main/version.json
RegExp=(?siU)^(.*?)$
OnConnectErrorAction=[!Log "Couldn't connect to the destination!"]
OnRegExpErrorAction=[!Log "RegExp Exception!"]

[Measure_Version_Parse]
Measure=Plugin
Plugin=JsonParser
Source=[Measure_Version]
Query="Ashuramaru_Bundle[0].version"
UpdateRate=500
DynamicVariables=1
OnUpdateAction=[!CommandMeasure "Script_Panel" "upgrade('[Measure_Version_Parse]')"] <-- This function just toggles meters basing on the provided value
User avatar
Yincognito
Rainmeter Sage
Posts: 7017
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Json Parser Plugin

Post by Yincognito »

Kurou wrote: May 25th, 2023, 2:57 pm Hi, im working on my suite which uses your JsonParser plugin to check for updates. I have one major problem with this plugin, it works how it should but every time the page in my panel loads this error is printed into rainmeter logs.
I don't know what could be the problem, since it doesn't seem like an misformatted query or anything like it (i.e. even commenting out the query altogether doesn't get rid of the error). Might be one of those initial errors at skin parsing / loading time, when everything is empty and you have side effects until the data is retrieved, considering we talk about a WebParser measure as a source. The curious thing is that starting with the plugin measure initially disabled should solve it, but for some reason, it doesn't.

That being said, there are some things that are not exactly optimal in your code, like the UpdateRate in the plugin measure instead of the WebParser one, or having an UpdateAction that, as it currently is, is executed every second, according to the Update value (and not the UpdateRate). Therefore, a better implementation would be along these lines (I replaced the OnUpdateAction with a Rainmeter log message to better see the differences):

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1

---Measures---

[Measure_Version]
Measure=WebParser
Url=https://raw.githubusercontent.com/Kurou-kun/Ashuramaru-Bundle/main/version.json
RegExp=(?s)^(.*)$
UpdateRate=500
OnConnectErrorAction=[!Log "Couldn't connect to the destination!"]
OnRegExpErrorAction=[!Log "RegExp Exception!"]
FinishAction=[!EnableMeasure Measure_Version_Parse][!UpdateMeasure Measure_Version_Parse][!UpdateMeter SomeMeter][!Redraw]
DynamicVariables=1

[Measure_Version_Parse]
Disabled=1
Measure=Plugin
Plugin=JsonParser.dll
Source=[Measure_Version]
Query="Ashuramaru_Bundle[0].version"
UpdateDivider=-1
OnUpdateAction=[!Log "Updated"]
DynamicVariables=1

---Meters---

[SomeMeter]
Meter=String
SolidColor=0,0,0,1
FontColor=100,255,100,255
FontFace=Consolas
FontSize=16
AntiAlias=1
MeasureName=Measure_Version_Parse
Text=Version: %1
UpdateDivider=-1
LeftMouseUpAction=[!EnableMeasure Measure_Version_Parse][!UpdateMeasure Measure_Version_Parse][!UpdateMeter SomeMeter][!Redraw]
DynamicVariables=1
So, in the above code, the plugin measure is enabled and triggered to update from the finish action of the WebParser one, as it's customary in these cases. This way, the plugin measure will update only when needed (e.g. some new data is retrieved via WebParser) according to its update divider and the finish action in the WebParser, instead of aimlessly do stuff once every second. I added a left mouse action in the meter to better see the fact that the error only appears at skin load / refresh time and not at subsequent mouse clicks, probably for the reasons mentioned above (even though it shouldn't happen, since the measure is initially disabled).

So, the error still occurs, but you shouldn't worry about it. The plugin developer should probably be more knowledgeable on why this happens and whether some fix is needed.
User avatar
Kurou
Posts: 40
Joined: January 24th, 2022, 2:54 pm
Location: World Wide Web
Contact:

Re: Json Parser Plugin

Post by Kurou »

Yincognito wrote: May 25th, 2023, 4:59 pm I don't know what could be the problem, since it doesn't seem like an misformatted query or anything like it (i.e. even commenting out the query altogether doesn't get rid of the error). Might be one of those initial errors at skin parsing / loading time, when everything is empty and you have side effects until the data is retrieved, considering we talk about a WebParser measure as a source. The curious thing is that starting with the plugin measure initially disabled should solve it, but for some reason, it doesn't.
Got it
Post Reply