skip to Main Content

I have a JSON input :

{
  "levelOne": [
    {
      "leveltwo": {
        "levelThree": [
          {
            "type": "typeOne",
            "leveltwo": {
              "Id": "101000094794",
              "Id2": "101000013207"
            }
          },
          {
            "type": "typeTwo",
            "leveltwo": {
              "Id": "101000013207",
              "Id2": "101000013207"
            }
          }
        ]
      }
    }
  ]
}

Is there a jolt spec that would lowercase every key, including keys in nested objects? (in this case what is under leveltwo)

{
  "levelone": [
    {
      "leveltwo": {
        "levelThree": [
          {
            "id": "101000094794",
            "id2": "101000013207",
            "type": "typeOne"
          },
          {
            "id": "101000013207",
            "id2": "101000013207",
            "type": "typeTwo"
          }
        ]
      }
    }
  ]
}

Currently, I’m using following spec (jolt template):`

[
  {
    "operation": "shift",
    "spec": {
      "levelOne": {
        "*": {
          "leveltwo": {
            "id": "&3[&2].leveltwo.id",
            "levelThree": {
              "*": {
                "leveltwo": {
                  "Id": "&6[&5].leveltwo.&3[&2].id",
                  "Id2": "&6[&5].leveltwo.&3[&2].id2"
                },
                "type": "&5[&4].leveltwo.&2[&1].type"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&1.key",
        "@": "&1.value"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "levelOne": {
        "key": "=toLower"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": "@(1,key)"
      }
    }
  }
]

enter image description here

Expected result (key should be in lower levelThree, typeOne, typeTwo):

{
  "levelone": [
    {
      "leveltwo": {
        "levelthree": [
          {
            "id": "101000094794",
            "id2": "101000013207",
            "type": "typeone"
          },
          {
            "id": "101000013207",
            "id2": "101000013207",
            "type": "typetwo"
          }
        ]
      }
    }
  ]
}

Thanks!

3

Answers


  1. Hi Priyanka this spec will resolve your issue. You can use =toLower case function in modify-overwrite-beta operation.

    [
      {
        "operation": "shift",
        "spec": {
          "levelOne": {
            "*": {
              "leveltwo": {
                "levelThree": {
                  "*": {
                    "leveltwo": {
                      "*": { 
                        //unwraps the field key and value into 2 feilds(key,Value). 
                        "$": "levelOne.[#4].leveltwo.levelThree.[#2].leveltwo.key",
                        "@": "levelOne.[#4].leveltwo.levelThree.[#2].leveltwo.value"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }, {
        "operation": "modify-overwrite-beta",
        "spec": {
          "levelOne": {
            "*": {
              "leveltwo": {
                "levelThree": {
                  "*": {
                    "leveltwo": {
                      //Key is on right side using toLower to make it lowercase.
                      "key": "=toLower"
                    }
                  }
                }
              }
            }
          }
        }
      }, {
        "operation": "shift",
        "spec": {
          "levelOne": {
            "*": {
              "leveltwo": {
                "levelThree": {
                  "*": {
                    "leveltwo": {
                       // Forming the original fields from key and value fields
                      "value": "levelOne.[#6].leveltwo.levelThree.[#2].leveltwo.@(1,key)"
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can repeatedly apply the consecutive shiftmodifyshift trio per each object key(levelOne and levelThree in this case) which needs a case conversion such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "id": "&3[&2].&1.id",
                "*": {
                  "*": {
                    "*": {
                      "Id": "&6[&5].&1.&3[&2].id",
                      "Id2": "&6[&5].&1.&3[&2].id2"
                    },
                    "type": "&5[&4].&3.&2[&1].type"
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "$": "&1.key",
            "@": "&1.value"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": "=toLower"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "value": "@(1,key)"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "$": "&4.&3.&2.&1.key",
                  "@": "&4.&3.&2.&1.value"
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "key": "=toLower",
                  "value": {
                    "*": {
                      "type": "=toLower"
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "value": "&4[&3].&2.@(1,key)"
                }
              }
            }
          }
        }
      }
    ]
    

    Another approach might be as follows :

    [
      { // combine the attributes and subattributes of the innermost array under the common objects of the newly created array, namely "values"
        // while reforming a new array namely "key"
        "operation": "shift",
        "spec": {
          "*": { // the level of levelOne
            "*": { // the level of indexes of levelOne 
              "*": { // the level of leveltwo
                "*": { // the level of levelThree
                  "$3|$2|$1|$": "key",
                  "*": { // the level of indexes of levelThree
                    "type": "value[&1].&",
                    "level*": {
                      "*": "value[&2].&"
                    }
                  }
                }
              }
            }
          }
        }
      },
      { // convert all letters of the keys and values into lowercase
        "operation": "modify-overwrite-beta",
        "spec": {
          "k*": "=toLower",
          "v*": {
            "*": {
              "*": "=toLower"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "key": {
            "*": { // expand the members of the array to the integer index vs. values pairs
              "@": "&2.&"
            }
          },
          "value": {
            "*": "&1[&]"
          }
        }
      },
      {// construct the result by matching "value" array with stairs-style qualified values coming from the "key" array 
        "operation": "shift",
        "spec": {
          "key": {
            "@1,value": "@(1,0)[@(1,1)].@(1,2).@(1,3)"
          }
        }
      }
    ]
    
    Login or Signup to reply.
  3. You can be sure about all things to be lower with this jolt spec. You can modify each location, keys, or values in the modify step.

    Please run each spec to see what happened.

    [
      {
        "operation": "shift",
        "spec": {
          "*": { // levelOne
            "*": { // 0
              "*": { // leveltwo
                "*": { // levelThree
                  "*": { // 0, 1
                    "type": {
                      "$": "[&2].keys"
                    },
                    "*": {
                      "$5": "[&2].location",
                      "$4": "[&2].location",
                      "$3": "[&2].location",
                      "$2": "[&2].location",
                      "@(1,type)": "[&2].values",
                      "*": {
                        "$": "[&3].keys",
                        "@": "[&3].values"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "keys|values|location": {
              "*": "=toLower"
            },
            "locatio*": "=join('-',@(1,location))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "keys": "@(1,location).&1.&",
            "values": "@(1,location).&1.&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*-*-*-*": {
            "*": {
              "values": {
                "*": {
                  "@": "&(4,1)[&(4,2)].&(4,3).&(4,4)[&3].@(3,keys[&1])"
                }
              }
            }
          }
        }
      }
    ]
    

    Note: If you want to prevent to lower your values you can change this line keys|values|location in the modify, to this keys|location.

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