Put JSON data into html form input hidden?

Put JSON data into html form input hidden?

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

Stefan Bogdanescu

Founder & Senior Architect

Storing JSON in Hidden Inputs Without Breaking HTML

Embedding raw JSON inside an HTML input[type="hidden"] element is a common pattern when you need to pass complex state to the client without triggering a server round-trip on every interaction. However, doing so requires careful handling of character encoding. If you simply dump a serialized array or object directly into the value attribute, your application will likely fail at runtime because the HTML parser and JavaScript engine interpret special characters differently than they do in pure JSON strings.

The fundamental issue arises from how JSON represents control characters. In a valid JSON string, a newline character is represented as \n, and a tab as \t. When you serialize a PHP array using json_encode(), it outputs these escape sequences exactly as they should be for a JavaScript object literal. However, the browser's HTML parser sees those backslashes differently depending on how the attribute is quoted.

If you look at the problematic example from your investigation, the output contains raw control characters or unescaped newlines within the value attribute. When the browser parses the HTML, it often strips out or misinterprets these characters if they break the string literal logic. Specifically, if the JSON string itself contains a double quote (") or a backslash (\), and your HTML attribute is not properly escaped, the parser thinks the value ended prematurely or that the attribute is malformed.

The Solution: JSON_HEX_* Constants

The most robust way to solve this in PHP is to use specific encoding flags when serializing the data. You cannot simply rely on json_encode() without flags if you intend to place the result directly into an HTML attribute string.

You must enable hex escaping for quotes, apostrophes, and ampersands. This ensures that any character that could break the HTML attribute syntax is converted into a hexadecimal entity (like " instead of ") before it reaches the browser.

Here is the correct implementation pattern:

$data = [
    'name' => 'George Wassouf',
    'track_id' => 123,
    'meta' => ['artist' => 'George', 'album' => 'Kalamak']
];

// Encode with hex escaping for quotes and ampersands
$jsonPayload = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);

<input type="hidden" name="track_data" value="<?php echo $jsonPayload; ?>">

By using JSON_HEX_TAG, JSON_HEX_APOS, JSON_HEX_QUOT, and JSON_HEX_AMP, you guarantee that the resulting string is safe to embed directly into an HTML attribute. The browser will render this as a valid string literal, and when you retrieve it with jQuery or vanilla JavaScript via document.querySelector('input[name="track_data"]').value, you get a clean, unencoded JSON string ready for JSON.parse().

Handling Arrays and Complex Structures

One of your original concerns was representing arrays. The method above handles them perfectly because json_encode converts PHP arrays into standard JSON array syntax ([]). There is no limitation on the data structure; as long as the output fits within reasonable memory limits, you can store deeply nested objects or large arrays in a single hidden input.

The "ugly" aspect of putting massive JSON blobs into HTML is subjective, but technically, it is perfectly valid provided the escaping is correct. The alternative of using data-* attributes on anchor tags fails quickly when dealing with multi-dimensional arrays because HTML attribute values are generally limited to a single line in older parsers, and even modern ones struggle with unescaped newlines inside attributes.

Security Considerations

While this technique is useful for state management, it introduces security considerations you must respect. A hidden input is not secure storage. Any user can inspect the DOM via browser dev tools (Firebug, Chrome DevTools) and modify the contents of that value attribute before submission. If your application relies on this hidden data to authorize actions or pass secrets, this approach is fundamentally flawed.

Use hidden inputs only for data that is already visible in the current request context or data that does not require confidentiality. For authentication tokens or sensitive identifiers, always rely on server-side session management or secure headers rather than client-side storage.

Conclusion

Embedding JSON into an HTML form input is a viable strategy for passing complex client-side state, provided you escape the output correctly using JSON_HEX_* flags. This ensures that special characters do not break the HTML structure and that the JavaScript receiving the data remains valid. Avoid raw string concatenation; instead, leverage PHP's built-in encoding options to generate safe, parseable payloads.

Our network