I have the following json response on a website I monitor:

{"24hreward":55,"hashrate":66}

I use the sensor HTTP XML/REST value to parse some values from the JSON file: it doesn't work if I configure the sensor settings with node 24hreward. I get error message: Node 24hreward not found in xml result.

However, it works if I want to parse "hashrate". Maybe the leading number in the object name is the issue? Many thanks.


Article Comments

Hello,

Thank you for your message.

Regarding JSON, the property name should effectively start with a letter, an underscore (_) or a dollar sign($) as indicated here: https://google.github.io/styleguide/jsoncstyleguide.xml

If you can't change the property name, then I recommend to use the EXE/Script or EXE/Script Advanced sensor with a custom script such as the following:

Param (
    #[Parameter(Mandatory)]
    [string]$url = ""
)

function Invoke-Request {
    Param(
        $url = $script:url
    )
    Invoke-RestMethod $url -Method Get
}

try {
    $r = Invoke-Request
    write-host @"
<prtg>
<result>
<channel>24h reward</channel>
<value>$($r."24hreward")</value>
</result>
<result>
<channel>Hash rate</channel>
<value>$($r.hashrate)</value>
</result>
</prtg>
"@
   
} catch {
    write-host "<prtg>"
    write-host "<error>1</error>"
    write-host "<text>$($_.exception.message) At line : $($_.InvocationInfo.ScriptLineNumber)</text>"
    write-host "</prtg>"
    Exit 1
}

Regards.


Apr, 2021 - Permalink