0

I'm wondering how I can use PHP to parse the JSON below and output the parsed values into CSV format.

{
 "test":{"12345":"98765","56789":"54321"},
 "control":{"99999":"98765","88888":"98765"}
}

I would like to extract just the keys from the test array (i.e. 12345, 56789) and have them returned in CSV format. Is that possible to do using json_decode?

Please feel free to ask any questions if you need more information

3
  • all the values on one row or for each value one row? and do you want to output as web-page or locally to a file? Commented May 3, 2013 at 23:19
  • each value one row, please Commented May 3, 2013 at 23:19
  • 1
    What's CSV about one value/line though? Commented May 3, 2013 at 23:25

2 Answers 2

5
$json = json_decode($json, true);
fputcsv(fopen('file', 'w'), array_keys($json['test']));
Sign up to request clarification or add additional context in comments.

1 Comment

crap.. didnt see the true.. sorry buddy.. +1
0
$s = '{
 "test":{"12345":"98765","56789":"54321"},
 "control":{"99999":"98765","88888":"98765"}
}';


$json = json_decode($s, true);
echo implode("\n", array_keys($json['test']));

Would that do?

2 Comments

This worked, but did not output to a CSV file. Thanks though!
@585connor writing to a file is something you probably should read up on then. :) See se1.php.net/file_put_contents

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.