json_resolver
The JSON Resolver module provides a flexible service for resolving JSON
templates with dynamic token replacement. It enables developers to define JSON
payload templates with placeholders that are replaced with actual data from
submission arrays at runtime. This module is JSON structure agnostic.
Use case
You have webforms to capture data and dynamically prepare the payload with a predefined structure.
This is particularly useful for
- Building API payloads from webform submissions
- Transforming data structures for third-party integrations
- Creating dynamic JSON responses with configurable templates
- Mapping form data to external service requirements
Key Features
- Token-based template system with nested data support
- Multiple configurable template types
- Clean service-based architecture with dependency injection
- Admin UI for template management via Configuration → System → JSON Resolver
- Comprehensive logging and error handling
Requirements
This module requires no modules outside of Drupal core.
Installation
Install as you would normally install a contributed Drupal module. For further
information, see:
https://www.drupal.org/docs/extending-drupal/installing-modules.
Configuration
Navigate to Administration → Configuration → System → JSON Resolver Settings (admin/config/system/json-resolver)
Grant the "Administer JSON Resolver" permission to the appropriate roles at Administration → People → Permissions
Configure the token map and templates:
Token Map Configuration
Define token-to-field mappings in JSON format. This maps the tokens used in
templates to actual field names in your submission data.
Example token map
{
"amount": "donation_amount",
"email": "donor_email",
"firstName": "donor.first_name",
"card": "payment.credit_card.number"
}
How it works
- Keys are token names used in templates as {{tokenName}}
- Values are paths to data in submission arrays
- Supports dot notation for nested data access
Template Management
Add, edit, or delete JSON payload templates:
To add a template
- Enter a unique machine name (lowercase letters and underscores only)
- Provide the JSON template with token placeholders (e.g., {{amount}})
- Click "Add template"
To edit a template
- Modify the JSON in the "Existing templates" section
- Click "Save configuration"
To delete a template
- Click the "Delete" button next to the template
- Confirm deletion
Example unresolved JSON template
{
"transaction": {
"amount": "{{amount}}",
"currency": "{{currency}}"
},
"donor": {
"email": "{{email}}",
"name": "{{firstName}} {{lastName}}"
}
}
Usage Example
Architecture Overview
The module provides a service-based architecture following Drupal best practices.
Service ID: json_resolver.resolver
Interface: Drupal\json_resolver\Services\JsonResolverInterface
Implementation: Drupal\json_resolver\Services\JsonResolverService
Basic Token Resolution Example
use Drupal\json_resolver\Services\JsonResolverInterface;
class MyService {
protected $jsonResolver;
public function __construct(JsonResolverInterface $json_resolver) {
$this->jsonResolver = $json_resolver;
}
public function processData(array $submission_data) {
$template_name = "SAMPLE"; // Define your template name stored in the module config.
// Your submission data. This could be dynamic from your webform.
// This should match the token field map.
$submission_data = [
'amount' => '100.00',
'email' => '[email protected]',
];
// Resolve template with actual data
$result = $this->jsonResolver->resolve($template_name, $submission_data);
// Result: ['amount' => '100.00', 'email' => '[email protected]']
return $result;
}
}