1

I am trying to adapt the following JSON with jolt

[
  {
    "id": "abc",
    "colour": "black | orange | anthracite",
    "material": "polyester | aluminium | v2a | steel",
    "temp min": "45 °C",
    "temp max": "-35 °C",
    "category": "rope",
    "length": "18 m"
  },
  {
    "id": "def",
    "colour": "black | orange | anthracite",
    "material": "polyester | aluminium | v2a | steel",
    "temp min": "45 °C",
    "temp max": "-35 °C",
    "category": "various"
  },
  {
    "id": "ghi",
    "colour": "black | orange | anthracite",
    "material": "polyester | aluminium | v2a | steel",
    "temp min": "45 °C",
    "temp max": "-35 °C",
    "categorie": "rope",
    "length": "10 m"
  }
]

The JSON needs to be transformed as follows.

The featureName has to be added as attribute, as well as the featureUnit (if Unit is available).

That means every time the featureValue ends on a certain list of specified units "°C, m, cm, mm, Years" a Unit attribute has to be created and the unit has to be removed from the original value

[
  {
    "id": "abc",
    "featureName_colour": "colour",
    "featureValue_colour": "black | orange | anthracite",
    "featureName_material": "material",
    "featureValue_material": "polyester | aluminium | v2a | steel",
    "featureName_temp min": "temp min",
    "featureValue_temp min": "45",
    "featureUnit_temp min": "°C",
    "featureName_temp max": "temp max",
    "featureValue_temp max": "-35",
    "featureUnit_temp max": "°C",
    "featureName_category": "category",
    "featureValue_category": "rope",
    "featureName_length": "length",
    "featureValue_length": "18",
    "featureUnit_length": "m"
  },
  {
    "id": "def",
    "featureName_colour": "colour",
    "featureValue_colour": "black | orange | anthracite",
    "featureName_material": "material",
    "featureValue_material": "polyester | aluminium | v2a | steel",
    "featureValue_temp min": "45",
    "featureUnit_temp min": "°C",
    "featureName_temp max": "temp max",
    "featureValue_temp max": "-35",
    "featureUnit_temp max": "°C",
    "featureName_category": "category",
    "featureValue_category": "various"
  },
  {
    "id": "ghi",
    "featureName_colour": "colour",
    "featureValue_colour": "black | orange | anthracite",
    "featureName_material": "material",
    "featureValue_material": "polyester | aluminium | v2a | steel",
    "featureValue_temp min": "45",
    "featureUnit_temp min": "°C",
    "featureName_temp max": "temp max",
    "featureValue_temp max": "-35",
    "featureUnit_temp max": "°C",
    "featureName_categorie": "categorie",
    "featureValue_categorie": "rope",
    "featureName_length": "length",
    "featureValue_length": "10",
    "featureUnit_length": "m"
  }
]

I managed to solve the featureName Problem with the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].&",
        "*": {
          "$": "[&2].featureName_&",
          "@": "[&2].featureValue_&"
        }
      }
    }
  }
]

But I have no idea how to solve the featureUnit-Problem.

2
  • Hi do we know the names of attributes that needs to be added as Units? Or it will be varying every time with input. Commented 11 hours ago
  • Thank you for your Message..Unfortunately the namens of the Attributes are Not fix. They vary with Input. Commented 11 hours ago

1 Answer 1

2

Assuming the whole value patterns are the same as with the presented input, then consider the following spec to distinguish the value sets :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].&",
        "*": {
          "*|* * *": {//values with multiple spaces or the else case
            "$1": "[&3].featureName_&2",
            "@1": "[&3].featureValue_&2"
          },
          "* *": {//values with single space
            "$(0,1)": "[&3].featureValue_&2",
            "$(0,2)": "[&3].featureUnit_&2"
          }
        }
      }
    }
  }
]

Not the answer you're looking for? Browse other questions tagged or ask your own question.