0
votes

In TYPO3 8.7 I'm trying to generate JSON using FLUID.

I've created a page with dummy content and I've updated my TypoScript configuration for all pages in a folder.

TypoScript setup

[PIDinRootline = 10]
    page = PAGE
    page {
        typeNum = 0

        config {
            disableAllHeaderCode = 1
            disablePrefixComment = 1
            xhtml_cleaning = none
            admPanel = 0
            debug = 0
            metaCharset = utf-8
            additionalHeaders = Content-Type:text/json;charset=utf-8
        }
    }
[global]

I created a dummy JSON file as well, to test the output, before creating the actual content using FLUID:

api.json

{
    "hello": "world"
}

Now, this seems to work fine. But the output still includes the html- and body-elements.

Rendered output:

<html>
    <head></head>
    <body>
        {"hello": "world"}
     </body>
</html>

The documentations says:

If you want to output JSON, RSS or similar data with Fluid, you have to write the appropriate TypoScript which passes the page rendering to Extbase and Fluid respectively. Otherwise, TYPO3 will always generate the <head>- and <body>-section.

But I don't know what to do here actually. How can I get TYPO3 to not render the wrapping HTML-elements?

3
have you considered the JsonView that comes from TYPO3 by default? Or you can just use the StandAlone Class, where you render only the actual template without header and body - Aristeidis Karavas

3 Answers

1
votes

The problem was a new syntax in TYPO3 8 and newer. In newer versions, the additionalHeaders is no longer just a TEXT but an array with numeric indices. So to set the correct header type, you have to use this one:

additionalHeaders.10.header = Content-Type:application/json;charset=utf-8
0
votes

When you override the page object, you should remove all inside it before with page >. Additionally Fluid is a templating engine for HTML, I don't know if that fits for JSON output.

Better way:
I'd suggest to use a separate object than page and a different page type:

[globalVar = GP:type = 133]
  jsonOutput = PAGE
  jsonOutput {
    typeNum = 133

    config {
      ...
    }
  }
[global]
0
votes

What you are missing is this typoscript:

config.disableAllHeaderCode = 1

read the manual

as you want it on a special page you can use a special typoscript template for this page. For a pagetype it is a little more complicated as this configuration would work on all pagetypes.
if you want some config options only for a special pagetype you can add this config to the pagetype only like this:

json = PAGE
json.config.disableAllHeaderCode = 1

Be aware that fluid is more complicated if you output other structure than HTML. But it is possible! Have a special look on whitespace and braces ({})