Return consistent JSON results from API

Engage with the community of users including those using the open source solutions.
Includes Nagios Core, Plugins, and NCPA

Return consistent JSON results from API

Postby JGCG » Mon Dec 09, 2019 8:43 am

The JSON returned from the API returns inconsistent types depending on whether mutliple results are returned or not.

As an example, requesting the status of a single host returns an object but requesting the status of multiple hosts returns an array of objects
Would it not be better to return an array even if a single result is returned for consistency.

I'm developing a library in Go but before parsing the JSON, I'm having to apply some extract logic to check how many results are returned.
If only one result is returned, there are a few extra hoops to run through: first unmarshal (convert) the JSON in an independant Host struct (A), then create my array (B), and then add the Host struct (A) to the array (B).

An example is at the below playground link:
https://play.golang.org/p/2k3xZwO1cMo

Thanks.

Single result (no array)
Code: Select all
{
   "recordcount": "1",
   "hoststatus": {
      "@attributes": {
         "id": "737359"
      },
      "instance_id": "1",
      "host_id": "5154",
      "name": "hosta"
   }
}


Mutliple results are within an array
Code: Select all
{
  "recordcount": "2",
  "hoststatus": [
    {
      "@attributes": {
        "id": "737359"
      },
      "instance_id": "1",
      "host_id": "5154",
      "name": "hosta"
    },
    {
      "@attributes": {
        "id": "737358"
      },
      "instance_id": "1",
      "host_id": "5153",
      "name": "hostb"
    }
  ]
}
Last edited by JGCG on Thu Dec 12, 2019 6:50 am, edited 1 time in total.
JGCG
 
Posts: 44
Joined: Fri Sep 29, 2017 6:31 am

Re: Return consistent JSON results from API

Postby jomann » Mon Dec 09, 2019 12:26 pm

Thanks for pointing this out, this is due to the fact that the original results are being given in XML and converted to JSON for our API. I agree that it should stay as an array no matter how many of the objects there are in there. The conversion on our side is the problem and can be updated to work better.

I will go ahead and add a task for this in 5.7 but it won't be able to be added earlier due to the fact this change may break user's scripts.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
jomann
Development Lead
 
Posts: 606
Joined: Mon Apr 22, 2013 10:06 am
Location: Nagios Enterprises

Re: Return consistent JSON results from API

Postby JGCG » Tue Dec 10, 2019 7:27 am

jomann wrote:Thanks for pointing this out, this is due to the fact that the original results are being given in XML and converted to JSON for our API. I agree that it should stay as an array no matter how many of the objects there are in there. The conversion on our side is the problem and can be updated to work better.

I will go ahead and add a task for this in 5.7 but it won't be able to be added earlier due to the fact this change may break user's scripts.


Appreciate that, thanks jomann.
JGCG
 
Posts: 44
Joined: Fri Sep 29, 2017 6:31 am

Re: Return consistent JSON results from API

Postby lmiltchev » Tue Dec 10, 2019 5:35 pm

@JGCG, let us know if it is OK to close this topic. Thanks!
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
lmiltchev
QA Manager
 
Posts: 13446
Joined: Mon May 23, 2011 12:15 pm

Re: Return consistent JSON results from API

Postby JGCG » Thu Dec 12, 2019 6:28 am

lmiltchev wrote:@JGCG, let us know if it is OK to close this topic. Thanks!

Please do, thanks.

Edit: Just another observartion, fields with no value return '{}' (empty object), as opposed to an empty string or null, as with the 'alias' field below.

Code: Select all
{
   "recordcount": "1",
   "hoststatus": {
      "@attributes": {
         "id": "737359"
      },
      "instance_id": "1",
      "host_id": "5154",
      "name": "hosta"
      "alias": {}
   }
}
JGCG
 
Posts: 44
Joined: Fri Sep 29, 2017 6:31 am

Re: Return consistent JSON results from API

Postby lmiltchev » Thu Dec 12, 2019 9:09 am

Edit: Just another observartion, fields with no value return '{}' (empty object), as opposed to an empty string or null, as with the 'alias' field below.

Thanks for the feedback @JGCG! I forwarded it to our developers.

I will be closing this topic now. If you have any further questions/issues, please start a new thread.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
lmiltchev
QA Manager
 
Posts: 13446
Joined: Mon May 23, 2011 12:15 pm


Return to Community Support

Who is online

Users browsing this forum: Google [Bot], tatapbl4 and 25 guests