2

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!

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55

3 Answers3

0

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)"
                }
              }
            }
          }
        }
      }
    }
  }
]
0

You can repeatedly apply the consecutive shift-modify-shift 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)"
      }
    }
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
0

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.

Mohammadreza Khedri
  • 2,523
  • 1
  • 11
  • 22
  • could you please try with the help of operation only after spec ? – Priyanka Koushal Mar 30 '23 at 11:29
  • @PriyankaKoushal I couldn't understand what you meant. Do you need help understanding how each operator works? – Mohammadreza Khedri Mar 30 '23 at 12:07
  • Yes ,Actually I'm using jolt template with huge input in which i have output but for some nested key i'm not able to convert in lowercase so can we add new Operations not whole template ? – Priyanka Koushal Mar 30 '23 at 12:16
  • @PriyankaKoushal Can you share the real simplified input? – Mohammadreza Khedri Mar 30 '23 at 12:27
  • INPUT : {"id":"12345","date":"2023-03-15","item":[{"id":"1847546","name":"abcd"}],"levelOne":[{"id":"6789","place":"place_name","leveltwo":{"levelThree":[{"type":"typeOne","leveltwo":{"Id":"101000094794","Id2":"101000013207"}},{"type":"typeTwo","leveltwo":{"Id":"101000013207","Id2":"101000013207"}}]},"unit":{"id":"68538"},"inneritem":[{"id":"1847546601","name":"abcd"}],"pattern":[{"name":"DOMAIN1","value":"SOS"},{"name":"DOMAIN2","value":"POP"}]}]} – Priyanka Koushal Mar 31 '23 at 05:27
  • EXPECTED OUTPUT: {"id":"12345","date":"2023-03-15","item":[{"id":"1847546","name":"abcd"}],"levelone":[{"id":"6789","place":"place_name","DOMAIN1":"SOS","DOMAIN2":"POP","leveltwo":{"levelThree":[{"id":"101000094794","id2":"101000013207","type":"typeOne"},{"id":"101000013207","id2":"101000013207","type":"typeTwo"}]},"unit":{"id":"68538"},"inneritem":[{"id":"1847546601","name":"abcd"}]}]} – Priyanka Koushal Mar 31 '23 at 05:28
  • USING JOLT SPEC: [{"operation":"shift","spec":{"id":"id","date":"date","item":"item","levelOne":{"*":{"id":"&2[&1].&","place":"&2[&1].&","pattern":{"*":{"name":{"DOMAIN1":{"@2,value":"&6[&5].DOMAIN1"},"DOMAIN2":{"@2,value":"&6[&5].DOMAIN2"}}}},"leveltwo":{"id":"&3[&2].id","levelThree":{"*":{"leveltwo":{"Id":"&6[&5].leveltwo.&3[&2].id","Id2":"&6[&5].leveltwo.&3[&2].id2"},"type":"&5[&4].leveltwo.&2[&1].type"}}},"unit":{"id":"&3[&2].unit.id"},"inneritem":"&2[&1].inneritem"}}}}] – Priyanka Koushal Mar 31 '23 at 05:30
  • and below Operations i'm using: { "operation": "shift", "spec": { "*": { "$": "&1.key", "@": "&1.value" } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "key": "=toLower" } } }, { "operation": "shift", "spec": { "*": { "value": "@(1,key)" } } } – Priyanka Koushal Mar 31 '23 at 05:31