JSONata用于修改对象数组并重命名名称键。

huangapple go评论89阅读模式
英文:

JSONata to alter array of object and rename name keys

问题

尝试使用JSONata更改对象数组时,需要更改地址名称,并且有些项目根本不使用。我可以告诉您的是,如果我更改数组,它会将它们分组到一个单独的数组中,就像lineOne将是[{"MY HOUSE1","SUITE 11-1111"}],而if函数的工作方式与我所预期的不同。

期望结果:

{
    "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
                        }
                    ]
                }
            ]
        }
    ]
}

我尝试过的内容:

"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()
		]
	}

也可以尝试:

"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
			}
		]
	}
]
英文:

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
			}
		]
	}
]

答案1

得分: 0

你可以像这样使用map运算符

{
    "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 }]
        }
    }
}

在Stedi游乐场上查看它:https://stedi.link/VOR9BFi

英文:

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

huangapple
  • 本文由 发表于 2023年7月20日 22:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定