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

Count Values in a JSON Array Returned From a REST API Call

DZone's Guide to

Count Values in a JSON Array Returned From a REST API Call

When we use multiple tools and existing tool features, we open up new options in how we approach our testing. This can help us identify workarounds.

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

I set myself a Practice Test Exercise. You might want to try it yourself before reading the full text of this post.

Exercise Described

  • How many ways do you have to count the values in a JSON array returned from a REST API call?
    • find a REST API that returns a JSON array object e.g.
    • how can you identify how many "to-do's" are in the list

To take part, you'll need to:

I used Thingifier, created a bunch of "to-do's" and then used GET /todos to return an array of "to-do's"

I eventually came up with six different ways to count the number in the array before I stopped.

I learned some new stuff along the way and remembered some old stuff. I found this a useful exercise.

If you identify other ways to count the JSON than I did, then I'd be interested in learning, so send me a message or leave a comment somewhere.

Roots of the Exercise

I've been working on Thingifier, which at the moment exposes a REST API for a simple To-Do Manager

Elisabeth Hocke and Thomas Rinke bravely picked the app to perform some testing on and created a useful write-up.

One note in the write-up mentioned that it could be useful if the responses described how many items were returned.

e.g. GET /todos returns a list of to-do's, but if there are a lot of them then it can be hard to know how many are there.

Elisabeth and Thomas were using Postman to test the app, and Postman does not show how many items are returned.

I wrote some comments to Elisabeth and Thomas, and at the time, the only "workaround" I could think of was to use Insomnia because that can show a filter count in the responses.

I suggested this because:

  • feature requests for testing can be useful
  • they can take time to build
  • we often need workarounds

But my only option made me realize that I didn't have enough workarounds available in my mind to handle this situation.

I used this limitation as a Personal Test Improvement Exercise.

Here's the exercise I set myself:

How many ways are there to count the values in a JSON array returned from a REST API call?

I found six ways before I stopped.

  • Using the JSON filter in Insomnia Response view
  • Using the pretty print view in Postman console
  • Script in Tests in a request in Postman
  • Paste the JSON into a browser dev console
  • Paste it into an online JSON viewer
  • Send Requests/Response through a Proxy and use the Proxy viewer

I hadn't used the Postman scripting before, so I learned a new set of knowledge when I tried that out. I'll write up a fuller set of learnings on Postman scripting in a later post.

Using the JSON Filter in Insomnia Response View todos.length

We don't have to stick to one tool. Insomnia is a REST Client that I often use for Exploratory testing.

It has a handy filter facility in the Response, which I can use to show the number of to-do's returned.

Using the JSON filter in Insomnia Response view todos.length

Using the Pretty Print View in Postman Console

In Postman show the console with "View \ Show Postman Console"

Then, use the pretty print view to see how many items are in the JSON array.

Starts at 0 so when it shows 201 at the bottom, it means there are 202 to-do's in the returned array.

Script in Tests in a Request in Postman

I can write any arbitrary code I want in the Postman "Tests" or "Pre-request Script," and I can use that to support my testing, not just assert on values.

e.g. The code below parses the response and logs to the Postman Dev console the number of items returned in the "to-do's" array in the response.

var response = JSON.parse(pm.response.text());
console.log("length : " + response.todos.length);

There are many ways to hack this in Postman: in the request, at a folder level, at a collection level, or possibly re-using a script created by a collection. But basically, view the "Tests" functionality as "Code to run after request is completed" functionality.

Paste the JSON Into a Browser Dev Console

Paste directly and you might see the result output:

> {todos: Array(202)}

or paste it into a variable e.g.

var result = {...pasted JSON here...}
> result.todos.length
<- 202

Paste It Into an Online JSON Viewer

e.g. http://jsonviewer.stack.hu/

Send Requests/Response Through a Proxy and Use the Proxy Viewer

I configured Insomnia to route through Charles proxy and used the Charles JSON view to show the number of items in the array. Other proxies have similar functionality.

End Notes

This was triggered by the very useful work that Elisabeth Hocke and Thomas Rinke describe in their write up.

I wanted to expand the options available to me when testing.

If you are interested in API testing, then I have a book you might like to look at, called Automating and Testing a REST API.

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
integration ,tutorial ,rest api ,json ,rest api call ,postman ,json array ,insomnia

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}