bazaarvoice / jolt

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

Replace Key of JSON using JOLT #845

Open khajaasmath786 opened 5 years ago

khajaasmath786 commented 5 years ago

Hi,

I have to replace . with _ in json keys. Here is the json I have, can you please help on how to use JOLT expression here. { "object.type": "Entity", "object.name": "assignments", "membership.member.type": "Person", "membership.organization.id": "urn:instructure:canvas:course:131710000000003181", "membership.organization.type": "CourseOffering", "actor.extensions[\"com.instructure.canvas\"].user_login": "nvseshad", "actor.extensions[\"com.instructure.canvas\"].root_account_id": "131710000000000001", "actor.extensions[\"com.instructure.canvas\"].root_account_lti_guid": "YYzs3up2ikmNIyNSel3biohOBqVtaOJCALBEKdzq:canvas-lms", "actor.extensions[\"com.instructure.canvas\"].root_account_uuid": "YYzs3up2ikmNIyNSel3biohOBqVtaOJCALBEKdzq", "actor.extensions[\"com.instructure.canvas\"].entity_id": "1317" }

wisthy commented 5 years ago

first of all, your json input is malformed. ´´´ "actor.extensions["com... ´´´ you cannot have " in there. If you want it that way, it can be escaped with the backslash.

Now, assuming that the way to go, I see a quick and dirty solution (inspired from #345). Issue is that it's absolutely not generic and if you change the input by including more dots in the same entry, it will have to be updated too

[
  {
    "operation": "shift",
    "spec": {
      "*.*.*.*.*": "&(0,1)_&(0,2)_&(0,3)_&(0,4)_&(0,5)",
      "*.*.*.*": "&(0,1)_&(0,2)_&(0,3)_&(0,4)",
      "*.*.*": "&(0,1)_&(0,2)_&(0,3)",
      "*.*": "&(0,1)_&(0,2)",
      "*": "&"
    }
  }
]

which gives as output:

{
  "object_type" : "Entity",
  "object_name" : "assignments",
  "membership_member_type" : "Person",
  "membership_organization_id" : "urn:instructure:canvas:course:131710000000003181",
  "membership_organization_type" : "CourseOffering",
  "actor_extensions[\"com_instructure_canvas\"]_user_login" : "nvseshad",
  "actor_extensions[\"com_instructure_canvas\"]_root_account_id" : "131710000000000001",
  "actor_extensions[\"com_instructure_canvas\"]_root_account_lti_guid" : "YYzs3up2ikmNIyNSel3biohOBqVtaOJCALBEKdzq:canvas-lms",
  "actor_extensions[\"com_instructure_canvas\"]_root_account_uuid" : "YYzs3up2ikmNIyNSel3biohOBqVtaOJCALBEKdzq",
  "actor_extensions[\"com_instructure_canvas\"]_entity_id" : "1317"
}