-3

I am getting response from the API like this

[{"destination_url":"https://www.smh.com.au/business/the-economy/australia-s-workplaces-are-transforming-search-where-the-jobs-will-be-in-2024-20180418-p4zaal.html?promote_channel=edmail&mbnr=MjAyNTgyMjU&eid=email:nnn-13omn654-ret_newsl-membereng:nnn-04%2F11%2F2013-news_am-dom-news-nnn-age-u&campaign_code=13INO010&et_bid=29124074&list_name=40_smh_newsalert&instance=2018-04-18--20-11--UTC",
  "doc_id":"17014",
  "main_url":"http://click.email.fairfaxmedia.com.au/?qs=b3370d8980e989c9e39909e0291c60d501f8a164477f9e049f07626b2a9c7e79da7d38ad705d7713e024559ecf2f217f79f8c08f728ab7fe",
  "redirect_urls":"http://click.email.fairfaxmedia.com.au/?qs=b3370d8980e989c9e39909e0291c60d501f8a164477f9e049f07626b2a9c7e79da7d38ad705d7713e024559ecf2f217f79f8c08f728ab7fe||http://www.smh.com.au/business/the-economy/australia-s-workplaces-are-transforming-search-where-the-jobs-will-be-in-2024-20180418-p4zaal.html?promote_channel=edmail&mbnr=MjAyNTgyMjU&eid=email:nnn-13omn654-ret_newsl-membereng:nnn-04%2F11%2F2013-news_am-dom-news-nnn-age-u&campaign_code=13INO010&et_bid=29124074&list_name=40_smh_newsalert&instance=2018-04-18--20-11--UTC"
}]"

Now I have to access the doc_id from this. Here's what I've tried:

$landerProperties = $request->data;
$landerProperties = json_decode(json_encode($landerProperties,true),true);
$id = (int)$landerProperties[0]['doc_id'];
foreach ($landerProperties as $key => &$value) {
   unset($value['doc_id']);
}

But it's giving me an exception: Illegal string offset 'doc_id'. Please tell me how to access the data from here.

Nick
  • 138,499
  • 22
  • 57
  • 95
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
  • Include your best coding attempt -- this is part of what is expected of you. Currently, it appears that you have done no research and have made no attempt. StackOverflow is not a free coding service. This is likely to be hammered by a very general "How to parse json" duplicate. – mickmackusa May 16 '18 at 10:19
  • *I am trying* - We don't see any of your attempts in the question. – Romeo Sierra May 16 '18 at 10:19
  • Not enough information – duatis May 16 '18 at 10:20
  • @mickmackusa please check, i have added the code here. And if anybody doesn't paste the code sample it doesn't mean that he didn't tried anything. – RAUSHAN KUMAR May 16 '18 at 10:25
  • Many people abuse the volunteers here, we cannot assume that you are a good or bad poster -- it is best that you prove your question to be "worthy". – mickmackusa May 16 '18 at 10:26
  • This should work if you `json_decode` it. I just don't understand what's the point in doing `$landerProperties = json_decode(json_encode($landerProperties,true),true);` However this seems like a problem with your quoting and all... – Romeo Sierra May 16 '18 at 10:26
  • Does it give you a line number where the error is getting thrown? – Michael Smith May 16 '18 at 10:27
  • @RomeoSierra the simple json_decode() giving result as null – RAUSHAN KUMAR May 16 '18 at 10:28
  • It makes absolutely no sense to `json_encode` something and then immediately `json_decode` it again. You'll only end up with the same thing you started out with. – deceze May 16 '18 at 10:31
  • I just tried 1.Enclosing your JSON string with single quotes. 2.`$obj = json_decode($str)`; 3.`var_dump()` it. Works flawless.. – Romeo Sierra May 16 '18 at 10:36
  • There is nothing wrong with your code (once you get rid of the spurious `json_encode`) i.e `$landerProperties = json_decode($request->data,true);` Where there is a problem is in your JSON which has a spurious `"` on the end which prevents it being decoded. – Nick May 16 '18 at 10:37

1 Answers1

1

If you want to access the first, use this: (notice the single quotes around the json string)

Code: (Demo)

$json = '[{"destination_url":"https://www.smh.com.au/business/the-economy/australia-s-workplaces-are-transforming-search-where-the-jobs-will-be-in-2024-20180418-p4zaal.html?promote_channel=edmail&mbnr=MjAyNTgyMjU&eid=email:nnn-13omn654-ret_newsl-membereng:nnn-04%2F11%2F2013-news_am-dom-news-nnn-age-u&campaign_code=13INO010&et_bid=29124074&list_name=40_smh_newsalert&instance=2018-04-18--20-11--UTC","doc_id":"17014","main_url":"http://click.email.fairfaxmedia.com.au/?qs=b3370d8980e989c9e39909e0291c60d501f8a164477f9e049f07626b2a9c7e79da7d38ad705d7713e024559ecf2f217f79f8c08f728ab7fe","redirect_urls":"http://click.email.fairfaxmedia.com.au/?qs=b3370d8980e989c9e39909e0291c60d501f8a164477f9e049f07626b2a9c7e79da7d38ad705d7713e024559ecf2f217f79f8c08f728ab7fe||http://www.smh.com.au/business/the-economy/australia-s-workplaces-are-transforming-search-where-the-jobs-will-be-in-2024-20180418-p4zaal.html?promote_channel=edmail&mbnr=MjAyNTgyMjU&eid=email:nnn-13omn654-ret_newsl-membereng:nnn-04%2F11%2F2013-news_am-dom-news-nnn-age-u&campaign_code=13INO010&et_bid=29124074&list_name=40_smh_newsalert&instance=2018-04-18--20-11--UTC"}]';

echo (int)json_decode($json, true)[0]['doc_id'];

Output:

17014
mickmackusa
  • 43,625
  • 12
  • 83
  • 136