skip to Main Content

Given the following input:

[
  {
    "users": [
      {
        "external_id": "aaa",
        "first_name": "john",
        "last_name": "doe",
        "email": "[email protected]"
      },
      {
        "external_id": "bbb"
      },
      {
        "external_id": "ccc"
      }
    ],
    "message": "success"
  }
]

I need the following output:

[
  {
    "external_id": "bbb"
  },
  {
    "external_id": "ccc"
  }
]

I want to filter external_id that doesn’t have first_name,last_name and email associated with it
I just tried shift operation and it gets all the external_id.

2

Answers


  1. You can use this spec:

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "users": {
              "*": "=concat('',@(0))"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "users": {
              "*": {
                "{*=*}": {
                  "@": "data[].&(1,1).&(1,2)"
                },
                "{*=*,*": {
                  "@1": "r"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "data": {
            "*": {
              "*": {
                "*": {
                  "$": "[].&2"
                }
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. One option is to use ~ operator to check the existence of one of the attributes from first_name,last_name or email, in this case I’ve chosen first_name for the modify transformation spec. Then dive up to the innermost object to derive the matching values while making others null to be prepared to be extinguished within the next transformation spec such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "users": {
              "*": {
                "~first_name": {
                  "external_id": "=(@(2,external_id))"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "users": {
              "*": {
                "first_name": {// pick up the innermost attributes named first_name 
                  "external_id": "[#3].&"
                }
              }
            }
          }
        }
      },
      {// get rid of null components
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search