Inconsistent json parsing error between PHP and JS
10:42 12 May 2026

I have the following output from LMStudio... this short response throws an error, but longer responses do not...

{ 
  "id": "chatcmpl-d9to84kewpb1g03aqazfps",
  "object": "chat.completion",
  "created": 1778598994,
  "model": "openai/gpt-oss-20b",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hi there! How can I help you today?",
        "reasoning": "User says \"Hello\". It's informal. We respond conversationally.",
        "tool_calls": []
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 1335,
    "completion_tokens": 33,
    "total_tokens": 1368,
    "completion_tokens_details": {
      "reasoning_tokens": 13
    }
  },
  "stats": {},
  "system_fingerprint": "openai/gpt-oss-20b"
}

This is being processed by the CURL request with

    $jsonResult = json_decode($result, true);
    $content = $jsonResult['choices'][0]['message']['content'];
    return $content;

and is being processed to return to JS like this...

    
    $return_object = [
    "result" => $result,
    "other" => "this" 
    ];
    
    echo json_encode($return_object);

But when the result hits my JS here...

        if (response.ok == true) {
            
            const data = await response.json();
            
            if (data) {
                controller.abort();
                return data;
            } 
            else {
                console.error("Error: Unexpected response format", data);
                controller.abort();
                chatFail = 1;
            }
        }

        ele
        {
            console.error("Error: "+response.statusText+" "+response.status);
            controller.abort();
            chatFail = 1;
        }

the "await response.json()" throws an error... BUT LONGER RESPONSES DO NOT

API Error: SyntaxError: The string did not match the expected pattern.

A long element may also contain arrays:

    $return_object = [
    "result" => $result,
    "array" => $array_of_things 
    ];

I am at my wits end with this one... any help gratefully received.

javascript php json