-5

I want to change this response to array
This is value what i received via API: "{'db_password': 'TgD31F#4124!'}"
I would like to remove the quotes and only keep TgD31F#4124!

Code Example

$db = Http::get(HERE_URL);
return $db->body(); // received it here "{'db_password': 'TgD31F#4124!'}"
1

2 Answers 2

0

You should use json() method or object() method on your response variable. API Reference

json method: Get the JSON decoded body of the response as an array or scalar value.
object method: Get the JSON decoded body of the response as an array or scalar value.

Usage:

$response = Http::get("www.api.com/...");
$decodedBody = $response->json();

OR

$decodedBody = $response->object();

EDIT: All available methods are listed in the documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry , but still same error
Have you tried to use $decodedBody['db_password'] in order to access your desired field value? It seems like you're doing something wrong since this is the valid by documentation.
0

add datatype:json in ajax then response.db_password

else, try parsing it to json. let RESULT = JSON.parse(response) console.log(RESULT.db_password)

2 Comments

i cant do it in ajax just via php because it api response
possible 2 solutions you can try : $result = $db->body()->toArray(); or $result = json_encode($db->body()); return $result->db_password;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.