When and How to Use JSON Serialization in Postman
Tags: api, tutorial, datascience, productivity
Published: 2022-03-10 15:07:32

---
title: When and How to Use JSON Serialization in Postman
published: true
date: 2022-03-09 18:58:57 UTC
tags: api, tutorial, datascience, productivity
canonical_url: https://blog.postman.com/when-and-how-to-use-json-serialization-in-postman/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck1ty5eom91qyxnf7kp9.png
---

As a new member of Postman’s developer advocacy team, I was recently tasked with completing our [30 Days of Postman challenge](https://www.postman.com/postman/workspace/30-days-of-postman-for-developers/overview). This challenge is a wonderful walk-through of the [Postman API Platform](https://blog.postman.com/new-postman-api-platform-redefining-api-management-for-api-first-world/)’s main features, and it really helps you get familiarized with the many ways Postman is able to assist with your productivity.

I started livestreaming [my journey on Twitch](https://www.twitch.tv/ian_the_postmanaut), and early on I was curious why I needed to serialize data in memory variables to pass between requests, especially when building dynamic request bodies. When I examined the data in the [Postman Console](https://blog.postman.com/powerful-debugging-with-the-postman-console/), it _appeared_ that they were being stored in a JSON object, yet I was unable to access the values correctly on subsequent requests.

For viewers of [my Postman Blindfold Challenge](https://www.youtube.com/watch?v=iCiwIRvaZuA), we all learned that I _love_ to peel back the layers on what’s really going on inside a system. I have a background in development, specifically systems architecture, and so understanding how things communicate is something that greatly enhances my learning.

Let’s do a quick review of serialization and why it’s used, and then look into Postman a little deeper.

## What is serialization?

“Serializing” data is used to store and convert complex data types when writing and reading from memory. It is especially useful when sharing data between multiple systems which may not all share common data types. By turning these complex data structures into simpler types, it allows independent systems to convert data in ways that are agreed upon.

![A variable is given to the serializer, and turned into a string](https://blog.postman.com/wp-content/uploads/2022/03/Serialization-image-1-1000x641.png)
_A variable is given to the serializer and turned into a string_

A simple example is an array of numbers. When we serialize this data into a string, in this JavaScript example with `JSON.stringify()`, we can see how it is converted into a string that includes square brackets around our comma-delimited list of numbers:

```
let arrayOfNumbers = [1,2,3,4,5]

JSON.stringify(arrayOfNumbers)

// ‘[1,2,3,4,5]’

```

When we “deserialize” this data back into an array, we are reversing the process to convert a string into what our programming language will interpret as an array of simple integer types. In this example, we’re still using JavaScript, and calling `JSON.parse()`.

![A string is given to the deserializer and turned back into a complex data stype](https://blog.postman.com/wp-content/uploads/2022/03/Serialization-image-2-1000x588.png)
_A string is given to the deserializer and turned back into a complex data type_

 

```
let stringData = “[1,2,3,4,5]”

JSON.parse(stringData)

// [1, 2, 3, 4, 5]
```

Most programming languages will interpret simple data types, sometimes called “primitives,” in a straightforward way. These primitive data types include strings, numbers, boolean true/false, and then “unknown” types such as “undefined” or “null” values.

If you need to store data in a more complex way—such as an array, or a deeply nested object—then transporting that data from one place to another is more easily managed between systems if we serialize the data.

In fact, most APIs are already serializing data for us. The most common way that APIs will “package” their data right now is by using JavaScript Object Notation, or JSON, and sending it as a big string within the HTTP response body. This allows the sending system to transport data to your client software in a way that is a high-level, abstracted form of their data. Once you receive that JSON data, your application will need to convert that string back into a structure that it can interact with in a more natural way.

## When you should serialize your data in Postman

So why is it that we need to JSON-serialize our data some of the time, and not all the time?

As mentioned above, primitive types such as strings and numbers do not need to be serialized; only complex data types like arrays and objects will need serialization when saving them in variables. This applies not only to global and environment variables, but collection and local variables as well.

If you need to prepare a response and [send it in a “dynamic body”](https://www.postman.com/postman/workspace/postman-answers/documentation/9215231-b9133e48-73c3-4aa4-b189-e038ee4c5e00), you will need to serialize the data and make sure that your body data is set to “raw,” and can be set to either “Text” or “JSON” in the dropdown at the end of that row of options:

![](https://blog.postman.com/wp-content/uploads/2022/03/serialization-1.png)

It is worth noting, also, that setting the outgoing body to “JSON” here does not have any effect on the dynamic variable you build in your pre-request script. Setting the body to JSON instead of Text only manipulates the outgoing header, not the body content itself.

Trying to debug this with the Postman Console gets a little tricky, though. Logging is run at the same time the script is executed, so building a complex structure of data is going to show up in the console correctly. But the pre-request script and the building of the dynamic body are handled in two different places.

If we do not serialize the data that we build, we’ll notice that Postman will convert this to a string containing `[object Object]`:

![](https://blog.postman.com/wp-content/uploads/2022/03/serialization-2.png)

However, when we serialize this data using `JSON.stringify()`, we see that our data is interpreted properly:

![](https://blog.postman.com/wp-content/uploads/2022/03/serialization-3.png)

This code and testing was done using the [Postman Echo API](https://learning.postman.com/docs/developer/echo-api/), which echos back any data that you send to it.

## Why is this a necessary step?

Building dynamic bodies for outgoing requests in Postman’s “pre-request scripts” is one of the most common places you will use this serialization technique.

Another common time to serialize and deserialize your data is when sharing responses from one request to another request. When these API responses are more complex objects, serialization will convert this data into string format, which can be parsed before using it in another request. When you serialize and “persist” data from collections and environments to Postman’s servers, your team can use that data once they have deserialized the data using `JSON.parse()`.

Again, this serialization step is only necessary if you need to store and retrieve complex data types in collection or global variables, or save something (“persisting” the data) in your Postman environment.

But why? Postman builds and stores this variable for use between portions of the application, and may execute code and build things in an order or manner that you are not expecting. Postman does its own serialization of the data, since we cannot know what you intend to do with your data. By using `JSON.stringify()` and `JSON.parse()` yourself, you help Postman to store and interpret the data more easily, removing unexpected results in your application.

_Technical review by [Arlemi Turpault](https://blog.postman.com/author/arlemi/) and [Shamasis Bhattacharya](https://blog.postman.com/author/shamasis/)._

The post [When and How to Use JSON Serialization in Postman](https://blog.postman.com/when-and-how-to-use-json-serialization-in-postman/) appeared first on [Postman Blog](https://blog.postman.com).