Convert JSON String Representation to JSON Object

In this article, we will look into how to convert JSON string representation to JavaScript’s JSON object using one of the JavaScript built-in functions.

You might have seen some cases where JSON string literal is involved to store in database, filesystem and other storage systems. The most specific case is exchanging JSON data between different programming languages and deserializing the JSON string to native object of the targeted language. The most suitable example would be the Web API endpoints through which data is transferred in JSON formats between two different platforms. Likewise, there are more real world scenarios where JSON string literal is used significantly.

So, let’s get started with an example:

PHP

The preceding code written in PHP outputs a JSON formatted string representation in plain text format. We want to make it usable in JavaScript. But it cannot be used as a string value. It needs to be processed as a JSON object rather than a string. Let’s see how we can do that.

JavaScript

It is as simple as that. This is a basic example without much back-end details on database call and front-end details on how API call is made and JSON string is returned. We will not cover those details in this article since that would be beyond of this article’s intention. If you want to know front-end and back-end implementation of API completely with real case scenario, please let us know in the comment section. We will be glad and ready to post an article on the requested topic.