The toJSON() Parsley call allows users to create custom endpoints from Models and Item.
The Parsley method
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:
depthis an integer for hydration depth, max depth is 5
showMetasupports a boolean value of true or _false.
true_ provides all meta, routing, zuid details, and locale details.
falseprovides only content.
The method's parameters,
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
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
thisinside of the Item associated View will access the corresponding Item's content. The Parsley keyword
thiscan only be used on a Content Model generated View, as it points to the connected item. For example:
- Adding the method to the Parsley first() or last() calls can access a corresponding item in a Multi-Entry or Dataset Model. For example:
- The method can also be used in Pasley
eachloops to render a JSON object of each item. Inside the
eachloop, filters can be applied by adding Parsley if or where conditionals as well to only output desired objects. For example:
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.
G2 MOMENTUM LEADER