What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Problem parsing API results when using JSON due to non-unique keys

Votes:

0

When requesting data from the API, either historic or live, data is returned as an array of objects where each object represents a single item. The object is format as such:

{
"datetime":"12/1/2015 12:00:20 AM",
"datetime_raw":42339.2085654630,
"value":"1,308,422 KByte",
"value_raw":1339823865.0000,
"value":"178,703 kbit/s",
"value_raw":22337843.6979,
"value":"1,271,663 KByte",
"value_raw":1302183233.0000,
"value":"173,682 kbit/s",
"value_raw":21710290.6469,
"value":"36,758 KByte",
"value_raw":37640632.0000,
"value":"5,020 kbit/s",
"value_raw":627553.0510,
"coverage":"100 %",
"coverage_raw":10000
}

The problem with this is that when parsed, only the last key is visible. This is because the keys are not unique. It is therefore impossible to parse values from a JSON api result and this should be considered a bug.

I'd like to propose the following format, which would be an accurate representation based on the XML structure:

{
"datetime":"12/1/2015 12:00:20 AM",
"datetime_raw":42339.2085654630,
"values":[
  {
  "channel": "Traffic Total (volume)",
  "channelid": -1,
  "value":"1,308,422 KByte",
  "value_raw":1339823865.0000
  },
  { 
  "channel": "Traffic Total (speed)",
  "channelid": -1,
  "value":"178,703 kbit/s",
  "value_raw":22337843.6979
  },
  {
  "channel": "Traffic In (volume)",
  "channelid": 0,
  "value":"1,271,663 KByte",
  "value_raw":1302183233.0000
  }
...and so forth
],
"coverage": "100 %",
"coverage_raw": "0000010000"
}

The difference here is that the values are separate objects that are contained within an array, thus can be iterated and processed as necessary.

This can currently be done with XML using XSLT to convert to a parseable JSON string, however XML is bulkier and there's an obvious performance penalty.

api bug json

Created on Dec 9, 2015 2:57:19 PM

Last change on Dec 9, 2015 3:46:12 PM by  Torsten Lindner [Paessler Support]



2 Replies

Votes:

0

Hi there,

I will forward your feedback and suggestion to our development so that we can consider if we want to change the API in this way. Thank you!

Best regards, Felix

Created on Dec 11, 2015 9:28:09 AM by  Felix Saure [Paessler Support]



Votes:

0

Hi. It's worth noting that when parsing the XML as JSON, the same issue occurs. However, by sorting the <item> node by element name, the resultant JSON is usable, as described above.

Just for clarification.

For instance - Sorted <item> node:

<item>
  <datetime>...</datetime>
  <datetime_raw>...</datetime_raw>
  <value channel="Total" channelid="-1">...</value>
  <value channel="Something" channelid="0">.../<value>
  <value_raw channel="Total" channelid="-1">...</value_raw>
  <value_raw channel="Something" channelid="0">...</valure_raw>
</item>

This results in

value: [{channel: "Total", channelid: "-1", text:"..."},{channel: "Something", channelid: "0", text: "..."}]

The parsers typically create an array if the following node has the same name, which is the desirable result. Sorting value and value_raw might automatically resolve both xml and JSON outputs.

Created on Dec 16, 2015 2:55:12 PM

Last change on Dec 17, 2015 7:39:16 AM by  Felix Saure [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.