Categories
JavaScript Answers

How to escape a JSON string to have it in a URL with JavaScript?

Spread the love

Sometimes, we want to escape a JSON string to have it in a URL with JavaScript.

In this article, we’ll look at how to escape a JSON string to have it in a URL with JavaScript.

How to escape a JSON string to have it in a URL with JavaScript?

To escape a JSON string to have it in a URL with JavaScript, we can use the encodeURIComponent function.

For instance, we write

const str = encodeURIComponent(JSON.stringify(obj));

to convert the obj object to a JSON string with JSON.stringify.

Then we call encodeURIComponent with the JSON string to escape it to that it can be in a URL.

Conclusion

To escape a JSON string to have it in a URL with JavaScript, we can use the encodeURIComponent function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *