A lot of API’s have an option to output in XML format. But now i have come acoss an API that only displays the results in JSON format. Is there a way to convert this into XML? Sample voucherfeed JSON output:
[
{
"code": "GHDPC150",
"programId": 1383,
"type": "generic",
"startDate": "2008-04-22T00:00:00+01:00",
"expiryDate": "2011-12-31T23:59:59+00:00",
"commissionType": "default",
"description": "The code provides 5% off all purchases at Gardens and Homes Direct",
"destinationUrl": null,
"discount": "5% off all purchases",
"program_name": "Gardens and Homes Direct",
"networkId": "UK",
"currency": "GBP"
},
{
"code": "MowDirect20",
"programId": 573,
"type": "generic",
"startDate": "2008-05-07T00:00:00+01:00",
"expiryDate": "2012-12-31T23:59:59+00:00",
"commissionType": "default",
"description": "£20 off £300 spend using MowDirect20",
"destinationUrl": null,
"discount": "£20 off £300 spend",
"program_name": "MowDirect?",
"network": "UK",
"currency": "GBP"
}
]
Best,
Marco
Hi David,
Sorry this only outputs <objects/>
Here is the api link (please do not publish):
Best,
Marco
Hi Marco,
Ah - the URL contains parameters itself so the entire value will need to be URL encoded. Simply create the following test script:
<?php
header("Content-Type: text/plain");
print urlencode("YOUR_API_LINK");
?>
...and use the displayed url encoded version in the url= parameter of the json2xml.php URL.
Cheers,
David.
--
PriceTapestry.com
Hello David,
Your example works for me
http://www.example.com/scripts/json2xml.php?url=http://www.example.net/feeds/vouchers.json
but what to do if JSON file inside a ZIP of GZ ?
I mean vouchers.json in vouchers.zip, for example
http://www.example.com/scripts/json2xml.php?url=http://www.example.net/feeds/vouchers.zip
...it seems this case JSON into XML "on the fly" does not work.
Thank you if there is a solution,
Serge
Hello Serge,
Here's a version that uses the automation tool library unzip functions;
<?php
require("../includes/common.php");
require("../includes/automation.php");
header("Content-Type: text/xml");
$tmp = $config_feedDirectory."tmp.json";
file_put_contents($tmp,file_get_contents($_GET["url"]));
if (isset($_GET["zip"]))
{
switch($_GET["zip"])
{
case "zip":
automation_unzip_unzip($tmp);
break;
case "gz":
automation_unzip_gzip($tmp);
break;
}
}
$json = file_get_contents($tmp);
$objects = json_decode($json);
print "<objects>";
foreach($objects as $object)
{
print "<object>";
foreach($object as $k => $v)
{
print "<".$k."><![CDATA[".$v."]]></".$k.">";
}
print "</object>";
}
print "</objects>";
?>
To use, add the &zip=zip or &zip=gz parameter to the proxy URL e.g.
http://www.example.com/scripts/json2xml.php?url=http://www.example.net/feeds/vouchers.zip&zip=zip
Cheers,
David.
--
PriceTapestry.com
Hello David,
spent abt an hour trying variants - but this solution does not work, at the same time I get same good result with first variant without unzip/ungzip.
Please, cross-check - it seems an error exactly in this part of code...
{code saved}
Does this script work for you ?
Best regards,
Serge
Hello Serge,
My apologies I'm working mobile this week and was merging this with other examples - the following line;
file_put_contents(file_get_contents($_GET["url"]));
...should be;
file_put_contents($tmp,file_get_contents($_GET["url"]));
(corrected above)
If still no joy, let me know and I'll check it out further with you...
Cheers,
David.
--
PriceTapestry.com
Hello David, thank you ! I have a joy :))) - but now I have working zip only, not gz...
I mean if I use
samplereg_json.gz&zip=gz
I again have something ungzipped
...but with
samplereg_json.zip&zip=zip
everything is OK...
...may be something else necessary for this part where switch
<?php
if (isset($_GET["zip"]))
{
switch($_GET["zip"])
{
case "zip":
automation_unzip_unzip($tmp);
break;
case "gz":
automation_unzip_gzip($tmp);
break;
}
}
?>
Hello Serge,
I've double checked with both zip/gz on my test server and both were OK with the above code - could you double check the .gz is valid by unzipping manually on the command line? If it's looking OK let me know and I'll check it out further with you...
Cheers,
David.
--
PriceTapestry.com
Hello David, you're right as always :) your code is correct, but in my particular case I have to use another function
<?php
case "gz":
automation_unzip_phpgzip($tmp);
break;
?>
not standard automation_unzip_gzip - that was the reason, so everything solved and work !
Thank you !
Serge
Hi Marco,
Here's a fetching filter script that you can use to convert the JSON into XML "on the fly". Save the following code as scripts/json2xml.php:
<?php
header("Content-Type: text/xml");
$json = file_get_contents($_GET["url"]);
$objects = json_decode($json);
print "<objects>";
foreach($objects as $object)
{
print "<object>";
foreach($object as $k => $v)
{
print "<".$k."><![CDATA[".$v."]]></".$k.">";
}
print "</object>";
}
print "</objects>";
?>
And then as the source URL for your feed simply use the URL to json2xml.php on your server followed by the JSON URL in the url parameter, for example:
http://www.example.com/scripts/json2xml.php?url=http://www.example.net/feeds/vouchers.json
The format string of the converted feed will be xml|OBJECTS/OBJECT/ and should auto-detect correctly.
Hope this helps!
Cheers,
David.
--
PriceTapestry.com