ClickPesa Tech

Follow

ClickPesa Tech

Follow
Add Quotes to your Unquoted JSON Objects

Add Quotes to your Unquoted JSON Objects

Quickly convert Javascript Object to JSON Object without writing code

Omar's photo
Omar
·Dec 19, 2022·

2 min read

In this article, we will not discuss what JSON or Javascript objects are; or their differences in syntax or usage, many articles have already covered them.

One thing to keep in mind is that according to specification JSON property names must be double-quoted.

// Example: JavaScript Object
{
    id: "TF_CE8TUEPX2LA",
    status: "PENDING",
    failureReason: null,
    language: "en",
    createdAt: 1669797871000,
    updatedAt: 1669797872000,
    completedAt: null,
    depositInitiatedAt: null,
    cancelledAt: null,
    ...
}

// Example: JSON Object
{
    "id": "TF_CE8TUEPX2LA",
    "status": "PENDING",
    "failureReason": null,
    "language": "en",
    "createdAt": 1669797871000,
    "updatedAt": 1669797872000,
    "completedAt": null,
    "depositInitiatedAt": null,
    "cancelledAt": null,
    ...
}

The requirement to convert a JavaScript object into a JSON object frequently arises while developing websites. Often one needs to utilize a valid JSON object, for instance, when testing an endpoint with a JSON payload over an HTTP client like Postman or Insomnia.

There is no doubt that your IDE allows you to pick numerous keys and add double quotes to the beginning and end of each key, but imagine if you had 200~300 keys! 🤯

Additionally, you could perform the conversion using the JSON.stringify() method, but who wants that? Right?🥱

I'm not sure if you know this, but you can use the Console tool from Google Chrome DevTools to quickly convert Javascript Objects to JSON Objects.

Here are the steps...

  1. Copy Unquoted JSON Object on the clipboard

  2. Navigate to Chrome Console Tab

  3. Paste Unquoted JSON Object on the console

  4. Hit Enter

  5. Right-Click on the result object to see the options

  6. Use Copy object option to copy your valid JSON Object and go break the internet!

Copy Object on Chrome Console

More tips such as this one that helps you move quickly are shared every month on our Blog. Make sure you take a look at them.

 
Share this