Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing for an Empty JSON Document

DZone's Guide to

Testing for an Empty JSON Document

Learn how to use RESTful services to perform searches and return the results as JSON from AngularJS with http.

· DevOps Zone ·
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

I have a RESTful service that performs a search and returns the results as JSON, and if there’s no results I return an empty document, {}.

Calling this search from AngularJS with $http.get(), I get the returned JSON result and everything is good if I have a document containing data, but an empty document took a bit more Googling to work out how to detect it.

From this similar question, I don’t want to use jQuery in my AngularJS app if I can avoid it (although doesn’t AngularJS have a “jQuery lite” api that maybe I can use?), so I used this approach instead:

if(Object.keys(data) == 0) { ... }

Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool.

Topics:
architects ,devops ,javascript ,json ,angularjs

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}