Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

julianov's avatar

JSON decode problem

Hello All.

I get this string from an Http request.

   [{"ID":876231, "TYPE":"PERF","NUMBER":223245,"NAME":"JULIAN","LAST_NAME":"OV","STREET":"TENARIS"}]

The problem here is that I want to select just the name, so:

    $data=$response_user->body()['NAME'] ;
	return $data; 

But I get this console output:

TypeError: Cannot access offset of type string on string in file

So:

    $data=json_decode($response_user->body(), true)['NAME'] ;
	return $data;

and this is the console output:

  ErrorException: Undefined array key "NAME" in file

It there something I missing?

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

You can always check what it returns:

dd($data=json_decode($response_user->body(), true));

I bet it will be an array of items so you will need to iterate over it.. this will work but it is hard-coded to the first item:

$data=json_decode($response_user->body(), true)[0]['NAME'] ;

note the [0]

1 like

Please or to participate in this conversation.