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

Read why times series is the fastest growing database category.

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) { ... }

Learn how to get 20x more performance than Elastic by moving to a Time Series database.

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 }}