I have a struct named requestStruct that looks like this:
{"api_version":"1","SourceId":"99","CampaignId":"999","policy":{"zip":"60644","street_address":"43 Memory Lane","email":"[email protected]","currently_insured":"true","coverage_level":"2","current_carrier":"28","current_insured_duration":"7"},"drivers":[{"first_name":"Bat","last_name":"Man","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Boy","last_name":"Wonder","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Cat","last_name":"Woman","birth_date":"1966-06-06","homeowner":"true","sr22":"false"},{"first_name":"Bat","last_name":"Girl","birth_date":"1966-01-01","homeowner":"true","sr22":"false"}]}
This is JSON being sent to another company's API so I have no control over the structure or datatypes that they require. Every value needs to be string when it is converted to JSON but Coldfusion wants to convert a string like "99" to just the number 99 when using serializeJSON. So I am using StructSetMetaData to make sure they are converted into JSON as strings.
These statements work fine:
<cfscript>
metadata = {
api_version: {type:"string",name:"api_version"},
SourceId: {type:"string",name:"SourceId"},
CampaignId: {type:"string",name:"CampaignId"}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
<cfscript>
metadata = {
policy:{
keys:{
"zip":{type:"string",name:"zip"},
"email":{type:"string",name:"email"},
"currently_insured":{type:"string",name:"currently_insured"},
"coverage_level":{type:"string",name:"coverage_level"},
"current_carrier":{type:"string",name:"current_carrier"},
"current_insured_duration":{type:"string",name:"current_insured_duration"}
}
}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
But I cannot figure out how to do the same to the drivers part of the struct as it contains an array.
I tried this and it had no effect:
<cfscript>
metadata = {
drivers:{
keys:{
"homeowner":{type:"string",name:"homeowner"},
"sr22":{type:"string",name:"sr22"}
}
}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
I tried this and it had no effect:
<cfscript>
metadata = {
drivers:{
keys:{
"homeowner[0]":{type:"string",name:"homeowner"},
"sr22[0]":{type:"string",name:"sr22"}
}
}
};
StructSetMetaData(requestStruct,metadata);
</cfscript>
How can I set the metadata for the keys in the array of structs in the driver key?
I would do something like this. For array inside structure you need to provide the metadata in the same way meta data is provided for arraySetMetaData function.
<cfset requestStruct = {"api_version":"1","SourceId":"99","CampaignId":"999","policy":{"zip":"60644","street_address":"43 Memory Lane","email":"[email protected]","currently_insured":"true","coverage_level":"2","current_carrier":"28","current_insured_duration":"7"},"drivers":[{"first_name":"Bat","last_name":"Man","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Boy","last_name":"Wonder","birth_date":"1966-07-07","homeowner":"true","sr22":"false"},{"first_name":"Cat","last_name":"Woman","birth_date":"1966-06-06","homeowner":"true","sr22":"false"},{"first_name":"Bat","last_name":"Girl","birth_date":"1966-01-01","homeowner":"true","sr22":"false"}]}>
<cfscript>
metadata = {
api_version: {type:"string",name:"api_version"},
SourceId: {type:"string",name:"SourceId"},
CampaignId: {type:"string",name:"CampaignId"},
policy:{
keys:{
"zip":{type:"string",name:"zip"},
"email":{type:"string",name:"email"},
"currently_insured":{type:"string",name:"currently_insured"},
"coverage_level":{type:"string",name:"coverage_level"},
"current_carrier":{type:"string",name:"current_carrier"},
"current_insured_duration":{type:"string",name:"current_insured_duration"}
}
}
};
driversMetadata = {
items: []
};
for(driver in requestStruct.drivers){
driversMetadata.items.append({
"homeowner":{type:"string", name:"homeowner"},
"sr22":{type:"string", name:"sr22"}
});
}
metadata.drivers = driversMetadata;
StructSetMetaData(requestStruct, metadata);
</cfscript>