-1

I have a json context, i want some data from that json, My json is

{
"req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.demo.com\/{api-endpoint-to-fetch-object-details}\/\"}"
}
{
"req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.demo.com\/{api-endpoint-to-fetch-object-details}\/\"}"
}

I need action and api_url data from json. I try something and i get upto

 {"config": {"action": "test", "user_id": "219096271813", "endpoint_url": "http://debasisa.testbase.info/demo-push.php", "webhook_id": "430339"}, "api_url": "https://www.dem.com/{api-endpoint-to-fetch-object-details}/"}

But i have no idea, how to get action and api_url value from this content in PHP.

Debasish
  • 322
  • 6
  • 23
  • `json_decode` can help you. – urfusion Jul 08 '17 at 11:25
  • 3
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 08 '17 at 11:25
  • Thanx@urfusion, I use json_decode, but i get **{"config": {"action": "test", "user_id": "219096271813", "endpoint_url": "http://debasisa.testbase.info/demo-push.php", "webhook_id": "430339"}, "api_url": "https://www.dem.com/{api-endpoint-to-fetch-object-details}/"}**, how can i fetch **action** and **api_url** from this content – Debasish Jul 08 '17 at 11:29
  • @Debasish : check the answer. – urfusion Jul 08 '17 at 11:33

2 Answers2

1

Try this

$level1 = '{
"req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.demo.com\/{api-endpoint-to-fetch-object-details}\/\"}"
}';
$data = json_decode($level1);
$newdata = json_decode($data->req_payload);
$action = $newdata->config->action;
urfusion
  • 5,528
  • 5
  • 50
  • 87
0
$output = json_decode($input);

You can use $output->req_payload->config->action and $outout->api_url if your JSON is a single object. If it is an array, then you will have to iterate its elements. And if it is like the one you have shown in your question, then it is invalid JSON.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175