JSON Payload Generator
Sometimes it is necessary to represent data in Dataverse in a JSON format.
Feature Overview
Sometimes it is necessary to represent data in Dataverse in a JSON format. This is typically require din integration scenario's but there might be other valid scenario's too. In order to generate JSON easily from a record and its related records, we have built the Payload Generator.
Design Details
Usage
The Payload Generator is written as an unbound Custom API function in Dynamics. It uses a JSON configuration to inform it how to generate the JSON representation of the entity. The function has 3 input parameters:
- klevr_record
- klevr_configurationtype
- klevr_payloadconfig
The configuration can be specified in one of 3 formats (klevr_configurationtype):
- RawString - The payloadconfig must contain the JSON configuration as a string.
- WebResource - The PayloadConfig must specify a name of a webresrouce file to read to get the JSON configuration.
- Environment Variable - The PayloadConfig must specify a schema name of an environment variable that contains the configuration.
- Auto (new after v2.22.1) - The config will be checked against a webresource, if not found, then environment variable, and if not found, used as a raw string.
The configuration JSON can have the following parameters:
Entity: The schema name of the entity that this section of the config refers to. PrimaryKey: The schema name of the field containing the primary key. In most cases it is <entity>id, but differs for activities and some special entities. Alias: A user assigned alias for this entity. Used in subsequent blocks for field replacement LinkField: Used for parent relationships. This will navigate to the record referred to in this lookup. Filter: Used for child relationships. You can specify the Filter or Condition parts of standard fetch to filter the child records to include in the generation. Fields: Dictionary of Schemaname, Alias for the fields you wish to include. You need to include all fields used for links too, but you can leave their alias blank if you want to exclude them from the final payload. Parents: Array of configuration blocks of this same schema for parent records you wish to include Related: Array of configuration blocks of this same schema for any child records you wish to include
Example: This exaplme includes comments to explain the various parts. Comments are not supported in the final jsno passed to the function.
{
"Entity": "klevr_formsubmission", //This is the primary entity where the extraction will start. It must match the klevr_record entity reference parameter
"PrimaryKey": "klevr_formsubmissionid", //Specify the primary field for this entity.
"Alias": "FormSubmission", //Alias to refer to this entity. Can be used in field value replacements in filters
"Fields": { //Key Value pair: Schema name and output name. Note, for lookups 3 output fields will automatically be added: <name>: Guid of record, <name>Name: Primary field of related record, <name>TypeName: Logical name of the entity of target record. For picklists, <name> will be value and <name>Name will be the label
"activityid": "FormSubmissionId",
"klevr_formid": "",
"klevr_portaluserid": "SubmittedById",
"modifiedon": "ModifiedOn",
"regardingobjectid": "RegardingId",
"klevr_externalreference": "ExternalReference"
},
"Parents": [ //Array of config blocks for any parent records. Alookup will be followed to fin the single record, and the output will be part of this recor'd JSON, so ensure the field aliases are unique
{
"PrimaryKey": "klevr_formid",
"LinkField": "klevr_formid",
"Entity": "klevr_form",
"Alias": "form",
"Fields": {
"klevr_formid": "FormId",
"klevr_formnumber": "FormNumber",
"klevr_title": "Title",
"klevr_tags": "Tags",
"klevr_externalreference": "FormExternalReference"
}
}
],
"Related": [ //Array of related records. Here you need to specify a filter. Filters can be a single condition, or multiple wrapped inside a filter tag. Follow fetchxml convension. Use {{Alias.Fieldname}} to replace values of preivously extracted records
{
"Entity": "klevr_section",
"PrimaryKey": "klevr_sectionid",
"Filter": "<condition attribute='klevr_formid' operator='eq' value='{{FormSubmission.klevr_formid}}' />",
"Alias": "Section",
"Fields": {
"klevr_sectionid": "SectionId",
"klevr_sectionnumber": "SectionNumber",
"klevr_name": "Name",
"klevr_sortorder": "SortOrder",
"klevr_klevrformsvisibilitylevel": "VisibilityType",
"klevr_repeatable": "Repeatable",
"klevr_externalreference": "ExternalReference"
},
"Related": [ //Related records can also have parent or related records, nesting multiple levels deep
{
"Entity": "klevr_question",
"PrimaryKey": "klevr_questionid",
"Filter": "<condition attribute='klevr_sectionid' operator='eq' value='{{Section.klevr_sectionid}}' />",
"Alias": "Question",
"Fields": {
"klevr_questionid": "QuestionId",
"klevr_questionnumber": "QuestionNumber",
"klevr_name": "Title",
"klevr_responsetype": "ResponseType",
"klevr_sortorder": "SortOrder",
"klevr_externalreference": "ExternalReference"
},
"Related": [//Related records can also have parent or related records, nesting multiple levels deep. Here's an example of a more complex filter.
{
"Entity": "klevr_formresponse",
"PrimaryKey": "klevr_formresponseid",
"Filter": "<filter type='and'><condition attribute='klevr_formsubmissionid' operator='eq' value='{{FormSubmission.activityid}}'/><condition attribute='klevr_questionid' operator='eq' value='{{Question.klevr_questionid}}'/><condition attribute='klevr_responsetype' operator='ne' value='699960012' /></filter>",
"Alias": "Answer",
"Fields": {
"klevr_formresponseid": "AnswerId",
"klevr_response": "Answer",
"klevr_index": "Index",
"klevr_externalreference": "ExternalReference"
}
}
]
}
]
}
Function output
Finally, the function returns a string that contains the JSON representing the record and its related records with the field names you have specified in the config.
Last updated on