I think you can use JSON.stringify:

// after your each loop
JSON.stringify(values);
Answer from mimiz on Stack Overflow
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
jQuery
api.jquery.com › jQuery.parseJSON
jQuery.parseJSON() | jQuery API Documentation
As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.
🌐
.Software
learncodeweb.com › javascript › how-to-decode-and-encode-json-data-in-javascript
How to decode and encode JSON Data in JavaScript
Coding education platforms provide beginner-friendly entry points through interactive lessons. This guide reviews top resources, curriculum methods, language choices, pricing, and learning paths to assist aspiring developers in selecting platforms that align with their goals.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript - MDN Web Docs
August 28, 2026 - Since the JSON format doesn't support object references (although an IETF draft exists), a TypeError will be thrown if one attempts to encode an object with circular references.
🌐
Rahul Singla
rahulsingla.com › home › jquery – encode & decode arbitrary objects to and from json
jQuery - Encode & Decode arbitrary objects to and from JSON - Rahul Singla
March 16, 2018 - I was surprised to see jQuery’s lack of support for JSON encoding of objects (decoding of JSON serialized string to an object is easily handled by browser’s native eval method).
🌐
Openjs
openjs.com › scripts › data › json_encode.php
JSON Encoder/Decoder Functions For JavaScript
JSON(JavaScript Object Notation) is a lightweight computer data interchange format. It is a text-based, semi-readable format for representing simple data structures and associative arrays. The main advantage of using JSON is that it can be parsed into an object using very little code - and for this reason, many people(including me) prefer using it as the ajax response format.
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JavaScript JSON.stringify()
The JSON.stringify() method converts a JavaScript value into JSON text.
🌐
SitePoint
sitepoint.com › php
json_encode Or jQuery's getJSON - PHP - SitePoint Forums | Web Development & Design Community
March 2, 2012 - They both do the same thing, create a JSON object, though one does it on the server end while the other has the browser do all the work. For returning output, which is better for performance?
Find elsewhere
🌐
jQuery
api.jquery.com › jQuery.getJSON
jQuery.getJSON() | jQuery API Documentation
Description: Load JSON-encoded data from the server using a GET HTTP request.
Top answer
1 of 4
9

It depends a lot on what you want todo with it, but this is a basic way of accessing the element keys. You can simply use the dot operator for each element key: "data.email" etc.

$.ajax({
    type: 'POST',
    url: 'support.php',
    success: function(result) {
        var data = jQuery.parseJSON(result);
        alert(data.email);
    }
});

INSERT INTO HTML ELEMENT:

I created a div with id="landingPad" and swapped out the alert line with:

$('#landingPad').html(data.email);

MAKE A LIST OF THE DATA RECEIVED:

Then I changed my div into an unordered list:

<ul id="landingPad"></ul>

After changing the success function, it listed all the data received from support.php:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        url: 'support.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            $.each(data, function(index, value) {
                $("#landingPad").append("<li>" + value + "</li>");
            });
        }
    });
});

CREATE FORM ELEMENTS WITH AJAX DATA:

Next, I created the following form:

<form name="http://example.com/edit_my_values" action="post">
    <div  id="landingPad"></div>
    <input type="submit" name="go" value="Edit Values"/>
</form>

Then by editing the AJAX, I created a form on the fly with the values received:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        url: 'support.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            $.each(data, function(index, value) {
                $("#landingPad").append('<input type="input" name="'+index+'" value="'+value+'"/><br/>');
            });
        }
    });
});

INSERT DATA INTO AN EXISTING FORM:

Given the following form:

<form name="http://example.com/edit_my_values" action="post">
    <label for="error">Error </label><input type="text" name="error"/><br/>
    <label for="successInfo">Success </label><input type="text" name="successInfo"/><br/>
    <label for="email">Email </label><input type="text" name="email"/><br/>
    <label for="subject">Subject </label><input type="text" name="subject"/><br/>
    <label for="description">Description </label><input type="text" name="description"/><br/>
</form>

You can fill your fields with AJAX data as follows:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        url: 'support.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            $.each(data, function(index, value) {
                $('[name='+index+']').val(value);
            });
        }
    });
});
2 of 4
2

You can access an array in this way

$.ajax({
      type: 'POST',
      url: 'support.php',
      success: function(result) {
        $('#content1').html(result[0]);
      },
  });
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-js-object-to-json-string-in-jquery-javascript
How to Convert JS Object to JSON String in JQuery/Javascript? - GeeksforGeeks
August 30, 2024 - Converting a JavaScript object to a JSON string means using the JSON.stringify() method to transform the object into a JSON-formatted string.
Top answer
1 of 5
23

Code example with JSON.stringify:

<html>
    <head>
       <title>jQuery Test</title>
       <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
       <script type="text/javascript">
           $(document).ready(function() {
               $("#submit").click(function(){
                   $.ajax({
                       url: "text.php",
                       type: "POST",
                       data: {
                           amount: $("#amount").val(),
                           firstName: $("#firstName").val(),
                           lastName: $("#lastName").val(),
                           email: $("#email").val()
                       },
                       dataType: "JSON",
                       success: function (jsonStr) {
                           $("#result").text(JSON.stringify(jsonStr));
                       }
                   });
               });
           });
       </script>
    </head>

    <body>
        <div id="result"></div>
        <form name="contact" id="contact" method="post">
            Amount: <input type="text" name="amount" id="amount"/><br/>
            firstName: <input type="text" name="firstName" id="firstName"/><br/>
            lastName: <input type="text" name="lastName" id="lastName"/><br/>
            email: <input type="text" name="email" id="email"/><br/>
            <input type="button" value="Get It!" name="submit" id="submit"/>
        </form>
    </body>
</html>

text.php

<?php
    $amount      = $_POST["amount"];
    $firstName   = $_POST["firstName"];
    $lastName    = $_POST["lastName"];
    $email       = $_POST["email"];
    if(isset($amount)){
        $data = array(
            "amount"     => $amount,
            "firstName"  => $firstName,
            "lastName"   => $lastName,
            "email"      => $email
        );
        echo json_encode($data);
    }
?>
2 of 5
5

Your object is most likely being passed properly. It's the way you're capturing the result that returns [object object] as @Spudley explained. The console doesn't know how to display the construct but you can display specific attributes using the object.attribute syntax. Use console.log() on the JS side or the code below for a prettified output.

// Indent with tabs
// Data is the parameter sent to the success function in the ajax handler
JSON.stringify( data , null, '\t');

From How can I pretty-print JSON in (unix) shell script?

Also Temporarily remove dataType on the ajax handler if you sense there's a bug somewhere. Getting the ouput to show on a GET request should do. Change this back to POST for any operation that modifies something like a database delete or alter.

Lastly, modify the header as shown in @GroovyCarrot's answer. If you're using Chrome the extra whitespace seems to be a bug: Tab and pre wrapped around JSON output in Chrome

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-js-object-to-json-string-in-jquery-javascript
How to Convert JS Object to JSON String in JQuery/ ...
September 19, 2026 - A JavaScript object can be converted into a JSON string using JavaScript's built-in JSON.stringify() method. This is useful when storing, transmitting, or sending object data through APIs.
🌐
Sololearn
sololearn.com › en › Discuss › 819410 › how-can-i-use-each-function-to-parse-json-data-using-jquery-that-comes-from-the-server-which-encode-using-php-json_encode
How can i use .each() function to parse json data using jquery that comes from the server which encode using php json_encode | Sololearn: Learn to code for FREE!
i cant parse using .each method but still am getting the data when in use consol.log() method i see the data in the browser inspect mode but when i use .each() error occur which says can not use "in " operator. plz help me. i use jquery ajax get function to send request to the server ... // jsonData => data from the server Object.keys(jsonData).forEach( function(key) { console.log(key); console.log(jsonData[key]); }); // hope this is what you are looking for
🌐
For the love of Code
vedanthk.wordpress.com › 2013 › 07 › 31 › getting-a-php-json_encode-object-into-jquery
Getting a PHP json_encode object into jQuery | For the love of Code
August 31, 2013 - Till line 8, the code has the usual database querying. Since this will return multiple objects, we need to put all these objects into an array. Then we encode that array. The array $json is an array having all values fetched from the database.
🌐
Stack Overflow
stackoverflow.com › questions › 27833900 › json-encode-in-php-and-retrieve-it-in-jquery
Json_encode in php and retrieve it in jquery
jquery.parsejson returns a js object from a json string. so after parsing you will get a js object. dont alert it check it in console using console.log(jquery.parsejson(data)); you will get to see the result.