Trying to alter an array of object with JSONata, addresses names need to be altered and some items are not used at all, what I can tell is if I alter the array it make groups them into an individual array like lineOne would be [{"MY HOUSE1","SUITE 11-1111"}]
and the if function does not work the way I would assume it should.
Source:
{
"customers": [
{
"title": "DR.",
"firstName": "FRED",
"middleName": "L.",
"lastName": "BEERMAN",
"suffix": "MR.",
"addresses": [
{
"addressLine1": "MY HOUSE1",
"addressLine2": "1366 ELM LANE",
"city": "EAST GREENBUSH",
"state": "NY",
"zip": "800131234",
"county": "SCHENECTADY",
"countryCode": "US",
"addressType": "RESIDENCE",
"sendMail": true,
"key": "0"
},
{
"addressLine1": "SUITE 11-1111",
"addressLine2": "1366 ELM LANE",
"city": "EAST GREENBUSH",
"state": "NY",
"zip": "12061",
"county": "SCHENECTADY",
"countryCode": "US",
"addressType": "SHIPPING",
"sendMail": true,
"key": "1"
}
]
}
]
}
Expectation:
{
"customers": [
{
"title": "DR.",
"givenName": "FRED",
"middleName": "L.",
"familyName": "BEERMAN",
"nameSuffix": "MR.",
"addresses": [
{
"addressType": "RESIDENCE",
"lineOne": "MY HOUSE1",
"lineTwo": "1366 ELM LANE",
"cityName": "EAST GREENBUSH",
"stateOrProvinceCountrySubDivisionId": "NY",
"postCode": "800131234",
"countyCountrySubDivision": "SCHENECTADY",
"countryId": "US",
"privacy": [
{
"okToContact": true
}
]
},
{
"addressType": "SHIPPING",
"lineOne": "SUITE 11-1111",
"lineTwo": "1366 ELM LANE",
"cityName": "EAST GREENBUSH",
"stateOrProvinceCountrySubDivisionId": "NY",
"postCode": "800131234",
"countyCountrySubDivision": "SCHENECTADY",
"countryId": "US",
"privacy": [
{
"okToContact": true
}
]
}
]
}
]
}
What I have tried:
"customerParty": {
"givenName": customers.firstName,
"middleName": customers.middleName,
"familyName": customers.lastName,
"title": customers.title,
"salutation": customers.title,
"addresses":[
customers.addresses.$each(function($value, $key) {
{
$key ? 'addressType' : 'addressType:' $value,
$key ? 'addressLine1' : 'lineOne:' $value,
$key ? 'addressLine2' : 'lineTwo:' $value,
$key ? 'city' : 'cityName:' $value,
$key ? 'countryCode' : 'countryId:' $value,
$key ? 'zip' : 'postCode:' $value,
$key ? 'state' : 'stateOrProvinceCountrySubDivisionId:' $value,
$key ? 'county' : 'countyCountrySubDivision:' $value,
$key ? 'sendMail' : '"privacy": [{"okToContact": $value}]:' ,
}
}) ~> $merge()
]
}
Also
"address":[
{
"addressType": customers.addresses.addressType,
"lineOne": customers.addresses.addressLine1,
"lineTwo": customers.addresses.addressLine2,
"cityName": customers.addresses.city,
"countryId": customers.addresses.countryCode,
"postCode": customers.addresses.zip,
"stateOrProvinceCountrySubDivisionId": customers.addresses.state,
"countyCountrySubDivision": customers.addresses.county,
"privacy": [
{
"okToContact": customers.addresses.sendMail
}
]
}
]
You can use the map operator like this:
{
"customerParty": {
"givenName": customers.firstName,
"middleName": customers.middleName,
"familyName": customers.lastName,
"title": customers.title,
"salutation": customers.title,
"addresses": customers.addresses.{
"addressType": addressType,
"lineOne": addressLine1,
"lineTwo": addressLine2,
"cityName": city,
"countryId": countryCode,
"postCode": zip,
"stateOrProvinceCountrySubDivisionId": state,
"countyCountrySubDivision": county,
"privacy": [{ "okToContact": sendMail }]
}
}
}
Check it out on the Stedi playground: https://stedi.link/VOR9BFi