Parsley toJSON()
The toJSON() Parsley call allows users to create custom endpoints from Models and Item.
The Parsley method toJSON()
enables users to create custom endpoints with a fully hydrated JSON object. It can also be used inline with JavaScript included in Views, providing additional access to a Content Item's stored data.
The toJSON()
method has two optional parameters, giving the user control over hydration level and the inclusion of metadata. This is achieved with the following syntax:
depth
is an integer for hydration depth, max depth is 5showMeta
supports a boolean value of true or _false.true
_ provides all meta, routing, zuid details, and locale details.false
provides only content.
The method's parameters, depth
and showMeta,
are optional. The default output of the method, without specification, will include meta data and a hydration depth level of 2.
Using the Method
The toJSON()
method can be used in a variety of ways to format the desired content and the URL routing location it will be rendered from. Below details how this can be accomplished in three different ways:
- Applying the Parsley method to
this
inside of the Item associated View will access the corresponding Item's content. The Parsley keywordthis
can only be used on a Content Model generated View, as it points to the connected item. For example:{{this.toJSON(depth, showMeta)}}
- Adding the method to the Parsley first() or last() calls can access a corresponding item in a Multi-Entry or Dataset Model. For example:
{{model.first().toJSON(2, true)}}
- The method can also be used in Pasley
each
loops to render a JSON object of each item. Inside theeach
loop, filters can be applied by adding Parsley if or where conditionals as well to only output desired objects. For example:
For examples of the toJSON()
method, please visit the Parsley REPL. Additional syntax details can be found in the Parsley Index.
Connect with Content Experts
Book a free 15-minute consultation with a content expert. Discuss your application, pain points and requirements. Understand how Zesty's lower total cost of ownership, features, functionality can elevate your business by creating extraordinary digital experiences.
Trusted By
G2 MOMENTUM LEADER