jsontransformjolt

write Jolt spec for nested arrays


I have a json with nested arrays, need to get the output in a specific format and I am not able to do so.

Input JSON :

{
  "audiencerange": [
    {
      "AudienceRangePrecision": [
        "03",
        "04"
      ]
    },
    {
      "AudienceRangePrecision": [
        "03",
        "04"
      ]
    }
  ]
}
In my conversion, O3 maps to "From", whereas 04 maps to "To".

The correct output should be like this:

{
  "onix_audience_range": {
    "values": [
      {
        "AudienceRangePrecision": "From,To"
      },
      {
        "AudienceRangePrecision": "From,To"
      }
    ]
  }
}

But through my jolt Spec I am getting :

{
  "onix_audience_range": {
    "values": [
      {
        "AudienceRangePrecision": "From,From"
      },
      {
        "AudienceRangePrecision": "To,To"
      }
    ]
  }
}

My spec is very big so just adding the following for AudienceRangePrecision.

{
  "operation": "shift",
  "spec": {
    "AudienceRangePrecision": {
      "*": {
        "1|01": {
          "#Exact": "onix_audience_range.values[&2].AudienceRangePrecision"
        },
        "3|03": {
          "#From": "onix_audience_range.values[&2].AudienceRangePrecision"
        },
        "4|04": {
          "#To": "onix_audience_range.values[&2].AudienceRangePrecision"
        }
      }
    }
  }
}

How do I achieve the correct Ouput?


Solution

  • You can use the following transformation :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "03": { "#From": "onix_audience_range.values[&4].&3[]" },
                  "04": { "#To": "onix_audience_range.values[&4].&3[]" }
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": {
    
                "*": "=join(',', @(1,&))"
    
              }
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here