You are here:  » JSON feed

Active Forum Topics


JSON feed

Submitted by marco on Mon, 2014-08-25 10:18 in

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

Submitted by support on Mon, 2014-08-25 11:41

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

Submitted by marco on Mon, 2014-08-25 12:45

Hi David,

Sorry this only outputs <objects/>

Here is the api link (please do not publish):

Best,
Marco

Submitted by support on Mon, 2014-08-25 12:50

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

Submitted by marco on Mon, 2014-08-25 13:41

Great as always!

Thanks,
Marco

Submitted by CashNexus on Sun, 2020-01-26 21:40

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

Submitted by support on Mon, 2020-01-27 08:44

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

Submitted by CashNexus on Mon, 2020-01-27 18:23

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

Submitted by support on Mon, 2020-01-27 18:31

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

Submitted by CashNexus on Mon, 2020-01-27 19:18

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;
    }
  }
?>

Submitted by support on Tue, 2020-01-28 09:55

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

Submitted by CashNexus on Tue, 2020-01-28 11:43

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