DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Count Values in a JSON Array Returned From a REST API Call

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.

Alan Richardson user avatar by
Alan Richardson
·
Aug. 01, 18 · Tutorial
Like (2)
Save
Tweet
Share
41.35K Views

Join the DZone community and get the full member experience.

Join For Free

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.


This article was syndicated from blog.eviltester.com. Author Alan Richardson is an Agile Software Development and Testing Consultant he has written 4+ books including Dear Evil Tester, Automating and Testing a REST API, Java For Testers. He has created 6+ online training courses covering Technical Web Testing, Selenium WebDriver and other topics. He is a prolific content creator on his blog, Youtube and Patreon.

JSON REST API Web Protocols Data structure

Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Steps for Getting Started in Deep Learning
  • 19 Most Common OpenSSL Commands for 2023
  • What Are the Different Types of API Testing?
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: