How to convert query string to json object in node js

Created at 01-Oct-2021 , By samar

How to convert query string to json object in node js

Through many examples, we will learn how to resolve the "How to convert query string to json object in node js".

You can convert query string to JSON object in node js app. You have to include querystring module and use parse method to get the object from query string URL.
  • Convert query string to json object in node js

    querystring = require('querystring');  
    const obj1=querystring.parse('name=samar&website=w3codegenerator');  
    console.log(obj1);
    

    Output:

    { name: 'samar', website: 'w3codegenerator' }

    Example code snippet

    const querystring = require('querystring');
    const url = "http://example.com/index.html?code=string&key=12&id=false";
    const object = querystring.parse(url);
    console.log(object);

    Output

    { 'http://example.com/index.html?code': 'string', key: '12', id: 'false' }

Back to code snippet queries related nodejs

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.