Javascript Convert PHP Json into a javascript array

Javascript Convert PHP Json into a javascript array

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden String Barrier in PHP JSON Responses

When you serialize a floating-point number using json_encode() in PHP, the resulting JavaScript object often arrives as a string rather than a native numeric type. If your backend returns ["float","float","float"], accessing Arr[0] yields "float" instead of a number you can use for calculations or comparisons. This isn't a bug in the language; it is a deliberate design choice driven by how JSON handles special values and how PHP's serialization functions interpret them.

The core issue lies in the distinction between standard floating-point numbers (like 3.14 or -2.5) and the PHP constant INF (infinity) or NAN (not a number). When you attempt to encode these specific values directly, they often get escaped into strings to maintain JSON validity across different parsers, or your PHP logic might be inadvertently casting them as strings before encoding.

To transform this string array into a usable JavaScript array of floats, you must parse the JSON string first and then handle the type conversion within your client-side code. The most robust approach involves using the JSON.parse() method to deserialize the incoming data. This step is non-negotiable; attempting to access the index directly on the raw response string will always fail because the entire payload is wrapped in quotes, making it a single string object rather than an array of objects.

Once deserialized, you have a JavaScript array where each element is still a string (e.g., "123.45"). To convert these strings back into actual Number types, you can iterate through the array and apply parseFloat() or the unary plus operator (+). The unary plus is often preferred for its brevity and clarity in intent:

const rawResponse = '["float","float","float"]';
const dataArray = JSON.parse(rawResponse);
const floatArray = dataArray.map(item => +item);

console.log(floatArray[0]); // Outputs the numeric value, not a string

However, if the source data literally contains the word "float" as text (as seen in your example), simply parsing the JSON won't magically turn that string into a number. In scenarios where json_encode is wrapping numbers as strings due to PHP configuration or specific version behaviors, you might need to inspect the incoming payload structure.

A common pitfall occurs when developers assume that JSON.parse() automatically types everything correctly. It does not. It strictly adheres to the JSON standard: if the input is "123", the output is a string. If the input was 123 (no quotes), the output would be a number. Therefore, the responsibility for type coercion falls entirely on the client-side logic after parsing.

If you are using Laravel, there are server-side configurations that can influence this behavior. Ensuring that your PHP environment correctly handles numeric types before encoding is often cleaner than fixing it in JavaScript. You might consider checking json_encode options or ensuring that the data being encoded isn't already a string array. For more complex serialization needs or to handle edge cases like infinity values more gracefully, exploring Laravel's built-in helpers or specific JSON configuration options can prevent these string-casting issues before they reach the frontend.

Ultimately, the solution requires a two-step process: parse the incoming JSON string into an object, and then explicitly coerce each element from a string type to a numeric type using parseFloat or type casting operators.

Our network