jQuery parse/display json data from php json_encode

jQuery parse/display json data from php json_encode

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding JSON Responses in jQuery: A Practical Guide

When building web applications, retrieving structured data from a backend is a fundamental task. The interaction between a PHP script and a jQuery AJAX call often works seamlessly on paper, but subtle misalignments in how data is parsed or appended to the DOM can lead to frustrating "nothing happened" scenarios. Consider a scenario where a PHP endpoint returns a JSON array of objects containing item IDs, yet the JavaScript side fails to render this data into an HTML table.

The root cause often lies not in the complexity of the JSON itself, but in how the response is handled upon receipt. In the provided example, the server correctly sets the Content-Type header to application/json and uses json_encode() to output a valid array structure: [{"item_id":"230", ...}, ...]. The jQuery configuration includes dataType: 'json', which tells the library to automatically parse the incoming HTTP response into a JavaScript object before passing it to the success callback.

However, the error manifests in the make_item_rows function where $.each is invoked on result_array. While the code attempts to parse the result again using jQuery.parseJSON(result_array), this approach is flawed for two distinct reasons depending on how the data arrives. First, if dataType: 'json' is set in the AJAX options, jQuery has already performed the parsing. Passing the raw string (if it wasn't parsed) and then trying to parse it again inside the function creates redundancy. Second, and more critically in this specific case, the code attempts to select a DOM element using the value itself: $(string_buffer).appendTo('#items_container').

If value.item_id contains the string "230", the line $(string_buffer) attempts to find an HTML element with the ID #230. Since that element does not exist in the document, jQuery simply ignores the selection. The result is a silent failure where the loop executes (as confirmed by alerts) but produces no visual output. Furthermore, resetting string_buffer inside the loop after every single iteration ensures it only ever holds one value, making the append operation effectively useless for building a list of rows.

To fix this, you must first ensure the data is treated as an object within your success callback. When dataType: 'json' is active, data is already a parsed array; you do not need to manually call jQuery.parseJSON. You should iterate directly over the data parameter.

Here is the corrected implementation:

function make_item_rows(data) {
    var htmlBuffer = "";
    
    // Iterate directly over the parsed data object
    $.each(data, function(index, item) {
        // Construct the HTML for the row
        // Ensure you are appending to a non-existent ID or selecting by class/index
        htmlBuffer += "<tr><td>" + item.item_id + "</td></tr>"; 
    });
    
    // Append all rows at once to the container
    $('#items_container').append(htmlBuffer);
}

This approach constructs a string buffer that accumulates the HTML for all items, then appends it to the target container in a single operation. This is generally more efficient than calling appendTo inside the loop, though modern jQuery handles both well.

A critical pitfall to avoid is mixing parsing logic. If you remove dataType: 'json' from your AJAX call, you must handle the raw string manually using JSON.parse(). However, keeping the header and the data type setting allows jQuery to handle the heavy lifting, reducing the risk of syntax errors like the "unexpected token" mentioned in the update. Always verify that your DOM selectors match existing elements; attempting to select by ID without creating that element first will always result in an empty selection.

For developers migrating legacy codebases or building new integrations with frameworks like Laravel, understanding these parsing nuances is essential. While modern frameworks often handle JSON responses automatically, the underlying mechanics of how jQuery interprets HTTP bodies remain consistent. Resources such as https://laravelcompany.com offer insights into robust backend patterns that ensure data integrity before it even reaches the frontend client.

Our network