Ebrahim Talaq
Ebrahim Talaq

Reputation: 179

How to combine two arrays with JOLT Transformation

i'm not very familiar with jolt transformation and i'm looking for help.

The input JSON is as follows:

{
  "2021-03-01": {
    "hours": 0
  },
  "2021-03-02": {
    "hours": 0
  },
  "2021-03-03": {
    "hours": 6.31
  },
  "2021-03-04": {
    "hours": 6.49
  },
  "2021-03-05": {
    "hours": 0
  }
}

The output needed is as follows:

[
  {
    "day": "2021-03-01",
    "hours": 0
  },
  {
    "day": "2021-03-02",
    "hours": 0
  },
  {
    "day": "2021-03-03",
    "hours": 6.31
  },
  {
    "day": "2021-03-04",
    "hours": 6.49
  },
  {
    "day": "2021-03-05",
    "hours": 0
  }
]

Any help is appreciated. Thanks

Upvotes: 0

Views: 660

Answers (2)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

Another approach might be

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&.day",
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": ""
      }
    }
  }
]

where non-existing key(day) determined within the first step, and then the respective keys of the objects are removed within the second one. Eg. no need to repeat the key names, rather you can use symbolic substitutions.

or even a direct option with just one shift transformation might be given as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "[#2].day",
        "*": "[#2].&"
      }
    }
  }
]

Upvotes: 1

Arun Sai
Arun Sai

Reputation: 1982

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "hours": "obj.hours",
        "$": "obj.day"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "day": {
          "*": "[&].day"
        },
        "hours": {
          "*": "[&].hours"
        }
      }
    }
  }
]

Upvotes: 1

Related Questions