0

I have a json response from an API and I can't create an array from it with "json_decode" in PHP to iterate through this as an array. I always get "NULL", when I use "var_dump" to print out, what my "json_decode" returns. The response-header of this API response is "application/json", but I am not familiar with this json format.

The json response from the API looks like this:

[1,"Example name","307","7","Test","455",1458572100000]
[1,"Another example name","146","7","Test","455",1458571500000]
[1,"Test","304","7","Test","455",1458572280000]
[1,"Example name 3","163","7","Hello world","455",1458571080000]

This is the result/response of a single API request. Now, for example, I want to get the penultimate number (in this case everytime 455) of every line/object or, for example, the name (second value: "Example name", "Another example name", "Test" and so on). How can I do this with php and this json format? It would be nice, if I can get an array from this to iterate through.

It's a local realtime bus arrival API, but it's similar to/the same as content.tfl.gov.uk/…and I call this API with a simple http/get request using file_get_contents in PHP

"/interfaces/ura/instant_V1?returnList=stopID,stopPointName,LineID,DestinationT‌​ext,estimatedTime,vehicleID&vehicleID=455")
10
  • 6
    you got all 4 of those lines back from the api? then that's not valid json. it's 4 separate json arrays stuck together, which is illegal syntax for json. you'd have to explode this string on the newlines, then decode each separate line as a distinct json string. Commented Mar 21, 2016 at 14:46
  • Is that ONE RESPONSE from the API or 4 seperate responses from 4 seperate calls to the API? Commented Mar 21, 2016 at 14:49
  • Yes, all 4 of those lines are in one single response from the api with the http header "application/json". Depending on my request parameters, I can also get, of cource, more than 4 lines, sometimes 10, 50 or even 500 of those lines within one response. Commented Mar 21, 2016 at 14:54
  • 1
    Then either the API is rubbish or you are chopping off some wrapper text that makes that into proper JSON. Did you remove a { and a } from the beginning and end of the string that gets returned? Commented Mar 21, 2016 at 14:57
  • You have a problem with your JSON, it is wrong formated. Commented Mar 21, 2016 at 14:59

1 Answer 1

0

To clarify, that's not JSON format that you're getting back. But if that's what you're getting then you need a solution :)

I would ignore the first & last character and use str_getcsv() to return the comma separated string as an array:

$input = '[1,"Example name","307","7","Test","455",1458572100000]';
$array = str_getcsv(substr($input, 1, -1));

If you are having multple lines, then you'll want to split them into individual lines before doing the above with:

$lines = explode($input, "]");
$array = array();
foreach($lines AS $line) {
    $input = '[1,"Example name","307","7","Test","455",1458572100000]';
    $array[] = str_getcsv(substr($input, 1, -1));
}

Updated to show delimeter of ] instead of \n as per comment below.

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

7 Comments

Thank you for your answer. This method works, but with "\n", I get my multiple lines (4) as one line and the php function "count" also returns "1", when I run it on $lines. Every "item" in my API response has it's own line, but inexplicably "\n" doesn't work for each line.
Now I used the last char "]" as delimeter and it works. Now I'm getting one array for each line.
Glad it's working. Updated answer with the different delimiter for you to accept the answer (although your example shows they are separated with new lines?).
If this works for you fine, but it is fixing a symptom of the problem and not fixing the real issue. As likely as not you will get situations that do not fit the workaround later on
Indeed, I indicate this in the answer. Let me make that message clearer for future visitors
|

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.