Jolt grouping and creating array

Hope you're all doing fine.

I'm brand new in jolt world, just started researching today. I'm having a lot of difficulty transforming my json result into the format that i'm supposed to send.

Here's an example of what i get:

[
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 1,
    "day": "Mon",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 1,
    "day": "Tue",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 1,
    "day": "Wed",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 1,
    "day": "Thu",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 1,
    "day": "Fri",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 0,
    "day": "Sat",
    "dzero": 0,
    "Active": 0
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 0,
    "day": "Sun",
    "dzero": 0,
    "Active": 0
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 1,
    "day": "Mon",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 1,
    "day": "Tue",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 1,
    "day": "Wed",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 1,
    "day": "Thu",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 1,
    "day": "Fri",
    "dzero": 1,
    "Active": 1
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 0,
    "day": "Sat",
    "dzero": 0,
    "Active": 0
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitoria",
    "d0": 0,
    "day": "Sun",
    "dzero": 0,
    "Active": 0
  }
]

And i need to group by what a consider the combination of "un", "uf", "city" and "d0" a key and group by them creating an array named "windows" with the remaining fields ("day", "dzero", "Active"). As described, my expected result would be the following:

[
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Cariacica",
    "d0": 1,
    "windows": [
      {
        "day": "Mon",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Tue",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Wed",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Thu",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Fri",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Sat",
        "dzero": 0,
        "active": 0
      },
      {
        "day": "Sun",
        "dzero": 0,
        "active": 0
      }
    ]
  },
  {
    "un": "RBA335",
    "uf": "ES",
    "city": "Vitória",
    "d0": 0,
    "windows": [
      {
        "day": "Mon",
        "dzero": 0,
        "active": 1
      },
      {
        "day": "Tue",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Wed",
        "dzero": 0,
        "active": 1
      },
      {
        "day": "Thu",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Fri",
        "dzero": 1,
        "active": 1
      },
      {
        "day": "Sat",
        "dzero": 0,
        "active": 0
      },
      {
        "day": "Sun",
        "dzero": 0,
        "active": 0
      }
    ]
  }
]

It would really help a lot if its possible to make it.

I thank you in advance!

Upvotes: 0

Views: 909

Answers (1)

mattyb
mattyb

Reputation: 12083

Try this following spec that uses this approach:

  1. Create a compound key and add it to each element
  2. Move all the elements with the same compound key into a key/value pair where the key is the compound key and the value is the array of elements with that compound key value.
  3. For the first element in each array, hoist the common fields up out of each element (except the compound key field). For each element in the array, push the day, dzero, and active fields down into a "windows" array.
  4. Put each element of each compound key object into a common top-level array.

.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "compound": "=concat(@(1,un),' ',@(1,uf),' ',@(1,city),' ',@(1,d0))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "@compound[]"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": {
          "day": "&2.windows[0].day",
          "dzero": "&2.windows[0].dzero",
          "Active": "&2.windows[0].active",
          "compound": null,
          "*": "&2.&"
        },
        "*": {
          "day": "&2.windows[&1].day",
          "dzero": "&2.windows[&1].dzero",
          "Active": "&2.windows[&1].active"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]

Upvotes: 4

Related Questions