skip to Main Content

There are multiple items in the array in which I am trying to remove the item whose idTypDesc is id if there is another item in array with idTypDesc as Ultimate ID. if the array doesn’t have item with idTypDesc as Ultimate ID then don’t remove the item whose idTypDesc is id

Input:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdType": {
          "idTypDesc": "id",
          "idTyp": "0004"
        }
      },
      {
        "AltIdVal": "hjn",
        "AltIdType": {
          "idTypDesc": "ultra ID",
          "idTyp": "0018"
        }
      },
      {
        "AltIdType": {
          "idTypDesc": "Ultimate ID",
          "idTyp": "0018"
        }
      }
    ]
  }
}

Expected Output:

{
  "PartyAlternateId": {
    "item": [
      {
        "AltIdVal": "hjn",
        "AltIdType": {
          "idTypDesc": "ultra ID",
          "idTyp": "0018"
        }
      },
      {
        "AltIdType": {
          "idTypDesc": "Ultimate ID",
          "idTyp": "0018"
        }
      }
    ]
  }
}

2

Answers


  1. You can optionally match through use of "":"" key-value pair within a shift ransforation spec such as

    [
      {
        "operation": "shift",
        "spec": {
          "PartyAlternateId": {
            "item": {
              "*": {
                "AltIdType": {
                  "idTypDesc": {
                    "id": {
                      "": ""
                    },
                    "*": {
                      "@2": "&6.&5.&4.&3" // replicates the expressin "PartyAlternateId.item.<index of the item array>.AltIdType"
                    }
                  }
                },
                "*": {//the elments other than "AltIdType"
                  "@1,AltIdType.idTypDesc": {
                    "id": {
                      "": ""
                    },
                    "*": {
                      "@2": "&6.&5.&4.y&3"
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "PartyAlternateId": {
            "item": {
              "*": "&2.&1[]"
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here

    Login or Signup to reply.
  2. It seems like the spec you provided only meets the condition when the Ultra id is present but it wont keep the id object if its not present. Can I suggest the following spec:

    [
    
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "idTypDesc": {
                    "id": {
                      //bucket AltIdType with Id idTypeDesc in IdBucket object and give it 
                      // order of 2 meaing second priority 
                      "@(3)": "&6.&5[#].IdsBucket.2"
                    },
                    "Ultimate ID": {
                      //bucket AltIdType with Ultimate Id idTypeDesc in IdBucket object and give it 
                      // order of 1 meaing first priority. 
                      //You can repeat as needed depending on how many values you want to match against
                      // and priortize. Lets say you have Super ID , then Id priority =3 , Ultra Id = 2 and Super Id = 1
                      "@(3)": "&6.&5[#].IdsBucket.1"
                    },
                    // Keep none Ids object untouched 
                    "*": {
                      "@(3)": "&6.&5[].&3"
                    }
                  }
                }
              }
            }
          }
        }
      }
      ,
      {
        // The sort will bring the item with the highest priorty (lowest number) on top
        // sort doesnt seem to work on an array of complex objects, otherwise IdsBucket
        // could have been made as an array and the next spec wont be required.
        "operation": "sort"
      }
        ,
      //make IdsBucket an array
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "IdsBucket": {
                  "*": "&4.&3.[&2].&1[]"
                },
                "*": "&3.&2.[&1]"
              }
            }
          }
        }
      }
       ,
      // select the 1st element (highest priorty id) from the IdsBucket and
      // store back under item array
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "IdsBucket": {
                  "0": "&4.&3[]"
                },
                "*": "&3.&2[]"
              }
            }
          }
        }
      }
    
     ]
    

    Even though the spec appears to be long but its really simple to follow:
    1- Bucket all id objects into IdsBucket parent object and assign each id object priority (the lower the number the higher in priority)
    2- Apply sort operation to bring item with highest priority (lowest number) to the top.
    3- Make IdsBucket an array. Unfortunately the sort above wont work if its already an array.
    4- select the first element (index 0) from the IdsBucket array above and move it back to the item array.

    If the sort happen to work on an array of complex objects, the 3rd spec wont be needed.

    Let me know what you think.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search