bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Help with key lookup #1177

Open arshad10244 opened 1 year ago

arshad10244 commented 1 year ago

Hi everyone,

I've spent quite a lot of time figuring it out but I'm stuck, i have a nested JSON and i want to enrich the values of "attr" with those matching the keys of "codes", please help :pray:

Here is my input :

{
  "items": {
    "a1b2xxxx": {
      "name": "item 1",
      "attr": [
        "A",
        "B",
        "C"
      ]
    },
    "c2b2cxxxx": {
      "name": "item 2",
      "attr": [
        "D",
        "E",
        "F"
      ]
    }
  },
  "codes": {
    "A": {
      "color": "green"
    },
    "B": {
      "size": "M"
    },
    "C": {
      "sku": "NS"
    },
    "D": {
      "stock": 2
    },
    "E": {
      "some_key": "some_value"
    },
    "F": {
      "foo": "bar"
    }
  }
}

This is the desired output :

{
  "items": {
    "a1b2xxxx": {
      "name": "item 1",
      "attr":  {
        "A" : {"color":"green"},
        "B" : {"size" : "M"},
        "C" : {"sku" : "NS"}
      }

    },
    "c2b2xxxx": {
      "name": "item 2",
      "attr": {
        "D" : {"stock": 2},
        "E" : {"some_key": "some_value"},
        "F" : {"foo" : "bar"}
      }
    }
  },
  "codes": {
    "A": {
      "color": "green"
    },
    "B": {
      "size": "M"
    },
    "C": {
      "sku": "NS"
    },
    "D": {
      "stock": 2
    },
    "E": {
      "some_key": "some_value"
    },
    "F": {
      "foo": "bar"
    }
  }
}
DhruvSingh861 commented 1 year ago

I think this is what you are trying to do (key look up) :-
spec :-

[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "a1b2xxxx": {
          "*": "items.a1b2xxxx.&",
          "attr": {
            "*": {
              "@": "items.a1b2xxxx.attr[].key"
            }
          }
        },
        "c2b2cxxxx": {
          "*": "items.c2b2cxxxx.&",
          "attr": {
            "*": {
              "@": "items.c2b2cxxxx.attr[].key"
            }
          }
        }
      },
      "*": "&"
    }
  }
  ,
  {
    "operation": "shift",
    "spec": {
      "items": {
        "a1b2xxxx": {
          "*": "items.a1b2xxxx.&",
          "attr": {
            "*": {
              "@(0,key)": {
                "*": {
                  "@(6,codes.&)": "items.a1b2xxxx.attr.@(3,key)"
                }
              }
            }
          }
        },
        "c2b2cxxxx": {
          "*": "items.c2b2cxxxx.&",
          "attr": {
            "*": {
              "@(0,key)": {
                "*": {
                  "@(6,codes.&)": "items.c2b2cxxxx.attr.@(3,key)"
                }
              }
            }
          }
        }
      },
      "*": "&"
    }
  }
]

output JSON :-

{
  "items" : {
    "a1b2xxxx" : {
      "name" : "item 1",
      "attr" : {
        "A" : {
          "color" : "green"
        },
        "B" : {
          "size" : "M"
        },
        "C" : {
          "sku" : "NS"
        }
      }
    },
    "c2b2cxxxx" : {
      "name" : "item 2",
      "attr" : {
        "D" : {
          "stock" : 2
        },
        "E" : {
          "some_key" : "some_value"
        },
        "F" : {
          "foo" : "bar"
        }
      }
    }
  },
  "codes" : {
    "A" : {
      "color" : "green"
    },
    "B" : {
      "size" : "M"
    },
    "C" : {
      "sku" : "NS"
    },
    "D" : {
      "stock" : 2
    },
    "E" : {
      "some_key" : "some_value"
    },
    "F" : {
      "foo" : "bar"
    }
  }
}