Anyone have a decent trick for importing generic JSON output? I’m currently considering using a JSON-to-XML script and importing with XML Importer, but maybe someone is already doing this?

I haven’t tried it but somebody wrote a Dynamic JSON datasource extension. Or do you want to import it into one of your Sections?

I’d suggest using the JSON Datasource extension that phoque mentioned, create a page using the datasource and do a copy-of in your XSLT to get all the XML from the datasource visible, then use the XML Importer extension to import it into sections…

A little bit of a long route, but I do similar for some of my data…

Hey, that’s pretty clever! I do want the data in a Section, but I think it might be cleaner to use a local standalone PHP converter script and then just call that URL with the XML Importer. (I will have to do this for more than one JSON feed, so the datasource+page approach might get somewhat tedious.)

You could just include the Json_to_xml class from my extension in your script.

$xml = Json_to_xml::convert($json);

I’ve found that @brentburgoyne’s Json_to_xml class works quite well. The only change I made was to throw an exception instead of returning error.

I also added in an option to get the data back as XML, or as an XML Document (the difference being one returns XML fragment omitting the XML declaration.

@brendo I am glad you have found it useful. I had also planned on having it throw an exception instead of returning an error. The other idea I had was to have an option for it to return the DOMDocument object itself, but I also see how it could be useful to return a fragment. I made a quick change to the class that will allow for all three.

Json_to_xml::convert($json) // returns XML with the declaration (same as before)

Json_to_xml::convert($json, 'fragment') // returns XML without the declaration

Json_to_xml::convert($json, 'object') // returns DOMDocument object

I gave this class a go, but it died on me while attempting to create the XML output. Not sure yet if this was a problem with the JSON source (maybe it had invalid characters or something.) Will followup here once I’ve got it sorted.

I did some more research on this, and one other approach is to use the PEAR XML Serializer as described here. I’m going to try this solution as well.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details