jsonjolt

add value to key within jolt


within jolt I try to transform the following jolt

Input :

{
  "id" : "76a3f8ae311327d684133d29fc620a7d",
  "property.country_of_origin.0" : "TW",
  "property.colour.0" : "black",
  "property.colour.1" : "orange"
}

Expected Output :

{
  "id": "76a3f8ae311327d684133d29fc620a7d",
  "properties": [
    {
      "id": "property.country_of_origin.TW"
    },
    {
      "id": "property.colour.black"
    },
    {
      "id": "property.colour.orange"
    }
  ]
}

That means, that the value of a property* should become the last part of the key ("TW" instead of 0, "orange" instead of 1 etc). After this transformation, this key should appear as value after "id" as a single object within a properties-Array.

Any help or hint would be highly appreciated.


Solution

  • The trick is separating the left hand side by dots while using concatenation operators for combining the dot separated pieces of the keys such as

    [
      {//derive independent "id" arrays
        "operation": "shift",
        "spec": {
          "*": "&",
          "*.*.*": {
            "*": {
              "$(1,1)|$(1,2)|@1": "&2.id"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "*y.*": {
            "@": "&(1,1)ies[]"
          }
        }
      },
      {//concatenate the components of the "id" arrays
        "operation": "modify-overwrite-beta",
        "spec": {
          "p*": {
            "*": {
              "*": "=join('.',@(1,&))"
            }
          }
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here