Return consistent JSON results from API

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
JGCG
Posts: 45
Joined: Fri Sep 29, 2017 6:31 am

Return consistent JSON results from API

Post by JGCG »

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.
jomann
Development Lead
Posts: 611
Joined: Mon Apr 22, 2013 10:06 am
Location: Nagios Enterprises

Re: Return consistent JSON results from API

Post by jomann »

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.
JGCG
Posts: 45
Joined: Fri Sep 29, 2017 6:31 am

Re: Return consistent JSON results from API

Post by JGCG »

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.
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: Return consistent JSON results from API

Post by lmiltchev »

@JGCG, let us know if it is OK to close this topic. Thanks!
Be sure to check out our Knowledgebase for helpful articles and solutions!
JGCG
Posts: 45
Joined: Fri Sep 29, 2017 6:31 am

Re: Return consistent JSON results from API

Post by JGCG »

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": {}
   }
}
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: Return consistent JSON results from API

Post by lmiltchev »

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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked