The Wayback Machine - https://web.archive.org/web/20240602093858/https://www.geeksforgeeks.org/how-to-select-values-from-a-json-object-using-jquery/
Open In App

How to select values from a JSON object using jQuery ?

Last Updated : 18 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will select values from a JSON object and display them on the browser using jQuery. To select values from a JSON object to webpage, we use append() method. 

This append() method in jQuery is used to insert some content at the end of the selected elements.

Syntax:

$(selector).append( content, function(index, html) )

Approach: First we store a JSON object containing (key, value) pair in a variable. We have created a button using <button> element and when we click the button, the jQuery function called with selector and click events. After click the button, the attr() method added the name attribute with <select> element. The <option> element is appended to each element using append() and each() methods.

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        How to select values from a
        JSON object using jQuery?
    </title>
  
    <!-- Import jQuery cdn library -->
    <script src=
    </script>
  
    <script>
        $(document).ready(function () {
  
            var json = [
                { "name": "GFG", "text": "GFG" },
                { "name": "Geeks", "text": "Geeks" },
                { "name": "GeeksforGeeks", 
                    "text": "GeeksforGeeks" }
            ];
  
            $('button').click(function () {
                var select = $("<select></select>")
                    .attr("name", "cities");
  
                $.each(json, function (index, json) {
                    select.append($("<option></option>")
                    .attr("value", json.name).text(json.text));
                });
                $("#GFG").html(select);
            });
        });  
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to select values from a
        JSON object using jQuery?
    </h3>
  
    <div id="GFG"></div>
  
    <button>Submit</button>
</body>
  
</html>


Output:

Before Click Button:

After Click Button:



Similar Reads

How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 min read
How to remove all the options of a select box and then add one option and select it using JQuery ?
Given a select box with a fixed number of options, the task is to remove all the options and then add one option and immediately select that particular option using jQuery. There are two approaches that can be taken to achieve this: Approach 1: Using find(), remove(), end() and append() Methods: In the following example, a select box with some opti
3 min read
Select different values on two select tags in Vue.js
In this article, we will see the selection of different values on a single select and two &lt;select&gt; tags in vue.js, along with understanding their basic implementation through the examples. HTML &lt;select&gt; tag is used to create a drop-down list. Adding a list of options inside the drop-down list using the &lt;option&gt; tag. when we select
6 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to Convert HTML Form Field Values to JSON Object using JavaScript?
Storing HTML input data into JSON format using JavaScript can be useful in various web development scenarios such as form submissions, data processing, and AJAX requests. Here we will explore how to convert the HTML form input data into the JSON format using JavaScript. ApproachThe HTML file contains a form with input fields for name, email, and me
2 min read
How to select first object in object in AngularJS?
The main problem that we are dealing with is that for an object of objects reading the object of a particular index position is not as simple as a list. We cannot loop over it using ngFor as an object is not considered an iterable. The importance of this issue may arise when the data received from any source is an object containing objects(like JSO
3 min read
How to Initialize a TypeScript Object with a JSON-Object ?
To initialize a TypeScript Object with a JSON-Object, we have multiple approaches. In this article, we are going to learn how to initialize a TypeScript Object with a JSON-Object. Below are the approaches used to initialize a TypeScript Object with a JSON-Object: Table of Content Object.assign Type AssertionSpread OperatorClass InitializationUsing
3 min read
How to Convert JS Object to JSON String in JQuery/Javascript?
Whenever we send the data to a web server, we send the data in the form of a string. To Convert JS Object to JSON String in JQuery/Javascript we can use the following approaches: Table of Content Using JSON.stringify() MethodUsing Lodash _.prototype.toJSON() MethodUsing jQueryMethod 1: Using JSON.stringify() MethodThe JSON.stringify() method in Jav
3 min read
How to Update JSON Object Value Dynamically in jQuery?
In jQuery, we can update JSON object values dynamically by manipulating the object directly or using utility functions. We will explore two approaches to update JSON object values dynamically in jQuery. Below are the approaches to update JSON object values dynamically in jQuery. Table of Content Using each() MethodUsing hasOwnProperty() methodUsing
3 min read