Take the 2-minute tour ×

Using PHP, how I can I parse a JSON object with an array given in square brackets and delimited by a pipe "|"?

For example, I need to access the second component of the "Data", in this case "English". Assume the lengths are variable, so you cannot slice by character positions.

 "Data":["53094185|English|USA|2012-07-24 12:49:00|AZ|"],
 "Location":"New York"}
share|improve this question
The value corresponding to the key of "Data" is an array with a single string element. JSON doesn't care about string delimiters. Grab the string, split it, and move on. –  Jack Maney Jul 24 '12 at 16:57
Thanks Jack - but what is the actual syntax, I can't seem to get it to actually print out the "Data" array, or any piece of it. –  Donny P Jul 24 '12 at 17:18
add comment

2 Answers

up vote 2 down vote accepted

Its simple:

$X = json_decode($JSON);
$arr = explode('|', $X->Data[0]);
share|improve this answer
Thanks Max, but the code doesn't work: <?php $jsontest = '{"Username":"5018", "Data":["53094185|English|USA|2012-07-24 12:49:00|AZ|"], "Location":"New York"}'; echo $jsontest; $parsed = json_parse($jsontest); $arr = explode('|', $parsed['Data']); print_r($arr); ?> –  Donny P Jul 24 '12 at 17:16
it should work fine now I misspelled a function name. It was json_decode not json_parse. Tested it now! –  MaX Jul 24 '12 at 17:19
awesome thanks :) –  Donny P Jul 24 '12 at 17:52
add comment

json_decode it. You will still need to split up the data array value string as this is just a simple string in JSON.

share|improve this answer
add comment

Your Answer


