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.

Answers 1
  • Convert query string to json object in node js

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

    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