Thursday, January 30, 2025
Google search engine
HomeLanguagesJavascriptHow to serialize an object to query string using jQuery ?

How to serialize an object to query string using jQuery ?

Given a jQuery object and the task is to serialize the object element into the query string using jQuery. To serialize an object we can use different approaches.

Below are the following approaches:

  • Using JSON.stringify() method and jQuery() param() Method
  • Using keys() and map() Methods

Approach 1: Using JSON.stringify() method and jQuery() param() Method

  • Declare an object and store it in the variable.
  • Use JSON.stringify() method to convert the object into strings and display the string contents.
  • Use the param() method to serialize the object element as a query string and store it into a variable.
  • Display the serialized object as a query string.

Example: This example uses the param() method to serialize the object. 

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
          Serialize an object to query string
      </title>
      <script src=
      </script>
</head>
   
<body
    <h1 style="color:green;">
        neveropen
    </h1>
    <p>
        Click the button to serialize
          the object to query string;
    </p>
   
    <button>
        click here
    </button>
   
      <p id="result"></p>
   
    <script>
        const data = {
            param1: 'val_1',
            param2: 'val_2',
            param3: 'val_3'
        };
       
        JSON.stringify(data);
         
          $('button').on('click', function () {
            const result = $.param(data);
            $('#result').text(result);
        });
    </script>
</body>
 
</html>


Output:

Animation

Approach 2: Using keys() and map() Methods

  • Declare an object and store it in the variable.
  • Use JSON.stringify() method to convert the object into strings and display the string contents.
  • Click on the button to call the convert() function which converts the serialized object to a query string.
  • The convert() function uses the keys() and map() methods to convert the serialized object to a query string.

Example: This example creates a function which is taking each key, and value pair and appends them as a string to get the query string keys() and map() method

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        Serialize an object to query string
    </title>
    <script src=
    </script>
</head>
 
<body>
    <h1 style="color:green;">
        neveropen
    </h1>
    <p>
        Click the button to serialize
        the object to query string
    </p>
 
    <button>
        click here
    </button>
    <p id="result"></p>
 
    <script>
        const data = {
            param1: 'val_1',
            param2: 'val_2',
            param3: 'val_3'
        };
       
        JSON.stringify(data);
 
        function convert(json) {
            return '?' +
                Object.keys(json).map(function (key) {
                    return encodeURIComponent(key) + '=' +
                        encodeURIComponent(json[key]);
                }).join('&');
        }
        $('button').on('click', function () {
            const result = convert(data);
            $('#result').text(result);
        });
    </script>
</body>
 
</html>


Output:

Animation

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments