skip to Main Content

I want to move "id" field to each of element in "user_roles" field of the document below.
I used the spec below but it didn’t work as my expected, I’m not sure where I may have made an error in the JOLT specification.

Input:

{
      "id": "657a9dc588b19",
      "user_roles": [
        {
          "role": 2,
          "status": 1,
          "is_owner": true,
        },
        {
          "role": 5,
          "status": 1,
          "is_owner": true,
        },
        {
          "role": 3,
          "status": 1,
          "is_owner": true,
        }
      ]
    }

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "user_roles": {
        "*": {
          "_id": "user_roles[&0]._id",
          "*": "user_roles[&1].&"
        }
      }
    }
  }
]

Expect result:

{
  "user_roles": [
    {
      "id": "657a9dc588b19",
      "role": 2,
      "status": 1,
      "is_owner": true,
    },
    {
      "id": "657a9dc588b19",
      "role": 5,
      "status": 1,
      "is_owner": true,
    },
    {
      "id": "657a9dc588b19",
      "role": 3,
      "status": 1,
      "is_owner": true
    }
  ]
}

2

Answers


  1. You can use the spec below with just a minor change in your spec.

    [
      {
        "operation": "shift",
        "spec": {
          "user_roles": {
            "*": {
              "@2,id": "user_roles[&1].id",//go one level up and get the id value
              "*": "user_roles[&1].&"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. There is one more way to achieve the expected output.

    [
      {
        "operation": "shift",
        "spec": {
          "user_roles": {
            "*": {
              "@2,id": "&2[&1].id",
              "*": "&2[&1].&"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search