skip to Main Content

I am having trouble dealing with arrays in JOLT.

Input:

[
  {
    "id": "1",
    "home_phone": [
      "55555555",
      "66666666"
    ]
  },
  {
    "id": "2",
    "home_phone": [
      "77777777",
      "88888888"
    ]
  }
]

Desired output:

[
  {
    "id": "1",
    "home": "55555555"
  },
  {
    "id": "1",
    "home": "66666666"
  },
  {
    "id": "2",
    "home_phone": "77777777"
  },
  {
    "id": "2",
    "home_phone": "88888888"
  }
]

Note that each element of the home_phone array should be matched with its correspondent id in its JSON document.

This is the spec I have drafted so far without much success:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "home_phone": {
          "*": "[].phone",
          "@(1,id)": "[].id"
        }
      }
    }
  }
]

Any ideas? Thanks!

2

Answers


  1. You can create your objects in an array of an array in one shift operation and then you can bring them up with just going to that level with "*": "" in the second shift operation.

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "home_*": {
              "*": {
                "@(2,id)": "[&3][&1].id",
                "@0": "[&3][&1].&(2,1)"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    
    Login or Signup to reply.
  2. One approach would be using

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "home_phone": {
              "*": { // partition the objects by indexes from different two levels 
                "@2,id": "&3_&1.id",
                "@": "&3_&1.&2"
              }
            }
          }
        }
      },
      { // add an array wrapper while getting rid of newly formed object labels
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search