JSON 데이터를 변환할 때 accepts_marketing과 email_marketing_consent 필드를 모두 처리해야 하는 경우가 있습니다. JOLT(JSON Language for Transform)를 사용하면 이러한 변환을 효과적으로 수행할 수 있습니다. 이 글에서는 두 필드를 적절히 처리하는 JOLT 스펙 작성 방법을 알아보겠습니다.
1. 문제 상황
다음과 같은 입력 JSON이 주어졌을 때:
{
"customer": {
"accepts_marketing": true,
"email_marketing_consent": {
"state": "subscribed",
"opt_in_level": "confirmed_opt_in",
"consent_updated_at": "2021-11-03T14:30:00-04:00"
}
}
}
우리는 이를 다음과 같은 형태로 변환하고자 합니다:
{
"marketing_consent": {
"email": {
"subscribed": true,
"opt_in_level": "confirmed_opt_in",
"updated_at": "2021-11-03T14:30:00-04:00"
}
}
}
2. JOLT 스펙 작성
이 변환을 수행하기 위한 JOLT 스펙은 다음과 같습니다:
[
{
"operation": "shift",
"spec": {
"customer": {
"accepts_marketing": "marketing_consent.email.subscribed",
"email_marketing_consent": {
"state": {
"subscribed": {
"#true": "marketing_consent.email.subscribed"
},
"*": {
"#false": "marketing_consent.email.subscribed"
}
},
"opt_in_level": "marketing_consent.email.opt_in_level",
"consent_updated_at": "marketing_consent.email.updated_at"
}
}
}
},
{
"operation": "default",
"spec": {
"marketing_consent": {
"email": {
"subscribed": false
}
}
}
}
]
3. 스펙 설명
- accepts_marketing 처리: customer.accepts_marketing 값을 marketing_consent.email.subscribed로 직접 매핑합니다.
- email_marketing_consent.state 처리: 'subscribed' 상태일 때만 true로 설정하고, 그 외의 경우 false로 설정합니다.
- opt_in_level 매핑: email_marketing_consent.opt_in_level을 그대로 marketing_consent.email.opt_in_level로 매핑합니다.
- consent_updated_at 매핑: consent_updated_at을 updated_at으로 이름을 변경하여 매핑합니다.
- 기본값 설정: 'default' 연산을 사용하여 subscribed 필드가 없을 경우 false로 설정합니다.
4. 추가 팁
- JOLT 스펙을 테스트할 때는 온라인 JOLT 테스터를 활용하면 편리합니다.
- 복잡한 변환의 경우, 여러 개의 변환 단계를 순차적으로 적용할 수 있습니다.
- 조건부 로직이 필요한 경우, JOLT의 'modify-overwrite-beta' 연산을 고려해 볼 수 있습니다.
결론
JOLT를 사용하여 accepts_marketing과 email_marketing_consent 필드를 처리하는 JSON 변환은 복잡해 보일 수 있지만, 적절한 스펙을 작성하면 효과적으로 수행할 수 있습니다. 이 방법을 통해 다양한 소스의 마케팅 동의 데이터를 일관된 형식으로 변환할 수 있으며, 데이터 통합 및 분석 작업을 더욱 효율적으로 수행할 수 있습니다.