Search

How do I generate a JSON page from datasource?

Contenttypemappings extension is probably have not updated since.

Yes, use Content Type Mappings to set the Content-Type header, and produce the desired JSON format using XSLT.

Oh, little wonder

Nice to find your Content Type Mappings work as opp to the outdated version.

I'm not sure sure how its actually work after set the page type to json in the Pages.

From the page, it will still produce the xml namespace although the page in the browser is indeed application/json

<?xml version="1.0"?>
...

Do I actually need to construct a JSON structure in the xsl file or it should generate automatically?

this might be useful, too.

You need to build it in your stylesheet - there is no automatic transformation.

oh, a minute too late. You actually have to transform the xml output to json using xslt.

[damn it, again!] :) If you wan't to retain formatted html (e.g. from a text area field), you'll also need to transform the html nodes into a string. this should do the trick.

Oh yeah, time to get my hands dirty...

happy gardening

use this to remove the <?xml version="1.0"> output:

<xsl:output omit-xml-declaration="yes"/>

There are other attributes that can be used, but this one will do the trick for this purpose.

See http://stackoverflow.com/questions/24065275/xsl-trying-to-get-json-to-work

Trying to get the XML..

<user-types>
    <section id="11" handle="user-types">User types</section>
    <entry id="9">
        <name mode="unformatted" handle="home-owner" word-count="2" lang="en" handle-en="home-owner"><![CDATA[Home owner]]></name>
    </entry>
    <entry id="7">
        <name mode="unformatted" handle="contractor-residential" word-count="2" lang="en" handle-en="contractor-residential"><![CDATA[Contractor (residential)]]></name>
    </entry>
    <entry id="8">
        <name mode="unformatted" handle="contractor-non-residential" word-count="2" lang="en" handle-en="contractor-non-residential"><![CDATA[Contractor (non-residential)]]></name>
    </entry>
    <entry id="10">
        <name mode="unformatted" handle="commercial-property-owner" word-count="3" lang="en" handle-en="commercial-property-owner"><![CDATA[Commercial property owner]]></name>
    </entry>
</user-types>

To be JSON, but the tag entry doent get turned into an array, so JS can only read one of them :(

current broken JSON output

{
"user-types" : {
    "section" : {
        "@attributes" : {
            "id" : 11,
            "handle" : "user-types"
        },
        "value" : "User types"
    },
    "entry" : {
        "@attributes" : {
            "id" : 9
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "home-owner",
                "word-count" : 2,
                "lang" : "en",
                "handle-en" : "home-owner"
            },
            "value" : "Home owner"
        }
    },
    "entry" : {
        "@attributes" : {
            "id" : 7
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "contractor-residential",
                "word-count" : 2,
                "lang" : "en",
                "handle-en" : "contractor-residential"
            },
            "value" : "Contractor (residential)"
        }
    },
    "entry" : {
        "@attributes" : {
            "id" : 8
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "contractor-non-residential",
                "word-count" : 2,
                "lang" : "en",
                "handle-en" : "contractor-non-residential"
            },
            "value" : "Contractor (non-residential)"
        }
    },
    "entry" : {
        "@attributes" : {
            "id" : 10
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "commercial-property-owner",
                "word-count" : 3,
                "lang" : "en",
                "handle-en" : "commercial-property-owner"
            },
            "value" : "Commercial property owner"
        }
    }
}
}

Using this XSL https://github.com/iwyg/xml-to-json

https://github.com/iwyg/apipage

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