JSON & REST APIs
Collection elements can work with data sources beyond WordPress content and GraphQL queries. You can use JSON arrays for static data and REST API responses for external data directly within Collection elements.
These data sources work exclusively with Collection elements and are not available in other contexts.
JSON Arrays
JSON arrays let you create static, structured data directly within Collection elements without needing WordPress posts or custom queries.
Basic JSON Syntax
- Simple Arrays
- Object Arrays
- Complex Data
Numbers:
[1, 2, 3, 4, 5]
Strings:
["Design", "Development", "Marketing", "Support"]
Mixed types:
["John Doe", 32, "Designer", true]
Key-value objects:
[
{"name": "John Doe", "role": "Designer", "experience": 5},
{"name": "Jane Smith", "role": "Developer", "experience": 8},
{"name": "Mike Johnson", "role": "Manager", "experience": 12}
]
Product catalog:
[
{"title": "Pro Plan", "price": 99, "features": ["Advanced Tools", "Priority Support"]},
{"title": "Starter Plan", "price": 29, "features": ["Basic Tools", "Email Support"]}
]
Nested structures:
[
{
"category": "Frontend",
"technologies": [
{"name": "React", "level": "Expert"},
{"name": "Vue", "level": "Intermediate"}
],
"projects": 15
},
{
"category": "Backend",
"technologies": [
{"name": "Node.js", "level": "Expert"},
{"name": "Python", "level": "Advanced"}
],
"projects": 22
}
]
Using JSON in Collection Elements
- Add Collection Element to your template
- Enter JSON data in the Data Source text area
- Add elements inside the Template element
- Bind data fields using
{{key}}syntax for object properties
Usage Examples
- Simple List Display
- Object Data Display
- Nested Data Access
<!-- JSON: pasted inside the Collection element
[
"Design",
"Development",
"Marketing"
]
-->
<!-- Collection template: -->
<div class="service-item">
<h3>{{.}}</h3>
</div>
<!-- JSON: pasted inside the Collection element
[
{
"name": "John",
"role": "Designer",
"experience": 5
}
]
-->
<!-- Collection template: -->
<div class="team-member">
<h3>{{name}}</h3>
<p>{{role}} - {{experience}} years experience</p>
</div>
<!-- JSON: pasted inside the Collection element
[
{
"category": "Frontend",
"technologies": [
{"name": "React", "level": "Expert"},
{"name": "Vue", "level": "Intermediate"}
],
"projects": 15
},
{
"category": "Backend",
"technologies": [
{"name": "Node.js", "level": "Expert"},
{"name": "Python", "level": "Advanced"}
],
"projects": 22
}
]
-->
<!-- Collection template: -->
<div class="skill-category">
<h2>{{category}}</h2>
<p>{{projects}} completed projects</p>
</div>
REST API Responses
Collection elements can consume REST API endpoints that return JSON arrays, enabling integration with external services and databases.
API Requirements
Response format: API must return a JSON array
Valid API response:
[
{"id": 1, "title": "First Post", "author": "John"},
{"id": 2, "title": "Second Post", "author": "Jane"}
]
Invalid API response:
{
"posts": [
{"id": 1, "title": "First Post"}
]
}
Setting Up REST API Data Source
- Add Collection Element to your template
- Enter API URL (must return JSON array)
- Add elements inside Template element
- Bind API response fields using
{{field_name}}syntax
Common API Integrations
GitHub repositories:
API URL: https://api.github.com/repos/WordPress/WordPress/contributors
Template: {{avatar}}, {{login}}, {{html_url}}
News feeds (JSON format):
API URL: https://wordpress.org/news/wp-json/wp/v2/posts
Template: {{title.rendered}}, {{excerpt.rendered}}, {{jetpack_featured_media_url}}
Product catalogs:
API URL: https://fakestoreapi.com/products
Template: {{title}}, {{price}}, {{description}}
Error Handling
- API unavailable: Collection displays empty if API request fails
- Invalid JSON: Collection displays error message
- Wrong format: Collection displays empty if response isn't an array
- Rate limits: Responses are cached to prevent excessive API calls
Choosing Your Data Source
- When to Use Each
- Performance Considerations
Use JSON Arrays when:
- Data is static and doesn't change frequently
- You need quick prototyping with sample data
- Building testimonials, team lists, or feature lists
- No external dependencies required
Use REST APIs when:
- Data changes frequently and needs real-time updates
- Integrating with external services (GitHub, social media, etc.)
- Building dashboards with live data
- Content comes from external systems
Use WordPress/GraphQL when:
- Data is managed within WordPress
- You need WordPress features (categories, custom fields, etc.)
- Building content-driven sites
- Leveraging WordPress's editing interface
JSON Arrays:
- Fastest: No network requests, immediate rendering
- Static: Data embedded in template
- Cache-friendly: Loads with page, no additional requests
REST APIs:
- Network dependent: Requires external API response
- Cached responses: Builderius caches API calls to improve performance
- Fallback behavior: Shows empty collection if API fails (user can provide conditional rendering for empty states)
WordPress/GraphQL:
- Database queries: Optimized for WordPress data
- Server-side caching: Leverages Builderius caching
- Dynamic: Updates automatically when content changes
These Collection data sources provide flexible options for displaying structured content without requiring WordPress posts or complex database queries, perfect for modern web development workflows that combine multiple data sources.
What's Next
- Collection Elements → - Learn more about Collection element functionality
- GraphQL Queries → - Explore more powerful data manipulation
- Expression Language → - Transform and calculate data values