How to render special formats (JSON)

Hi,

I’m currently trying to extend my frontend with a json view. I moved along with the example given in the documentation ( http://neos.readthedocs.org/en/latest/ExtendingNeos/RenderingSpecialFormats.html ).

The goal is to get the content of a single page by just replacing the default .html uri suffix with .json (download.html --> default html view, download.json --> json view).

So I added the following route in my Configuration/Routes.yaml:

-
  name: 'JSON view'
  uriPattern: '{node}.json'
  defaults:
    '@package': TYPO3.Neos
    '@controller': Frontend\Node
    '@action': show
    '@format': json
  routeParts:
    node:
      handler: TYPO3\Neos\Routing\FrontendNodeRoutePartHandlerInterface
  appendExceedingArguments: true

Furthermore I defined the type “json” as a typoscript root object.

json = TYPO3.TypoScript:Http.Message {
    httpResponseHead {
        headers.Content-Type = 'application/json'
    }
    content = TYPO3.TypoScript.value {
        value = 'Testing value just to check the output'
    }
}

I took the NeosDemoTypo3Org Site package for my development. Accessing the pages through *.html works fine, accessing the pages through *.json only works for the download page. All other pages render the default html-view although accessing them via the .json - route.

The implementation of the typoscript root object “json” seems to be ok - as all my changes work with the download page immediately (adding more fields, changing names etc.). I already tried figuring out whether there’s something special about the download page - but it doesn’t seem so.

Now my questions:

  1. Why does this only work with ONE page (the download page)?
  2. Is this the correct way of implementing a special frontend rendering format or is there any way to achieve this more easy?

Thank you in advance

P.S. all packages are the latest versions installed via composer (“typo3/neos”: “~2.1.0” etc.)…

Hi Sascha

It’s hard to tell without seeing all the code. Especially the root condition part. Also be aware that the root has a @cacheIdentifier, where you have to add {request.format} for it not to be cached.

The symptoms you’re having does sound like the content cache interferring and giving you unexpected results.

1 Like

Having a similar issue with rendering json as well. I followed the example code above as well and got

**An exception was thrown while Neos tried to render your page The TypoScript object at pathjsonTYPO3.TypoScript:Http.Message/contentcould not be rendered: The TypoScript objectTYPO3.Neos:TYPO3.TypoScript.valueis not completely defined (missing property@class). Most likely you didn't inherit from a basic object. For example you could add the following line to your TypoScript:prototype(TYPO3.Neos:TYPO3.TypoScript.value) < prototype(TYPO3.TypoScript:Template)json<TYPO3.TypoScript:Http.Message>/ For a full stacktrace, open Data/Logs/Exceptions/20161026060415e7655c.txt**

I’ve also tried defining a class similar to how this person approached it https://github.com/psmb/EncultDistr/tree/master/Packages/Sites/Sfi.Encult to no avail.

Is there a better way to go about creating a simple json api using typoscript?