How to convert query string to json object in node js

Updated 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