skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    In addition to the help provided by Barbaros, this spec does the job as well. It is alittle bit clumsy, I know :)

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[&1].&",
            "*": {
              "$": "[&2].&1.featureName_&1",
              "@": ["[&2].&1.featureValueTEMP0", "[&2].&1.featureValueTEMP1", "[&2].&1.featureValueTEMP2"]
            }
          }
        }
      }, {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "featureValueTEMP1": "=split(' ',@(1,&))",
              "featureValueTEMP2": "=split(' ',@(1,&))"
            }
          }
        }
      }, {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "featureValueTEMP1": "=lastElement(@(0))",
              "featureValueTEMP2": "=firstElement(@(0))"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[&1].&",
            "*": {
              "*": "[&2].&1.&",
              "featureValueTEMP1": {
                "m|°C": {
                  "@1": "[&4].&3.featureUnit_&3",
                  "@(2,featureValueTEMP2)": "[&4].&3.featureValue_&3"
                },
                "*": {
                  "@(2,featureValueTEMP0)": "[&4].&3.featureValue_&3"
                }
              }
            }
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "featureValueTEMP0": "",
              "featureValueTEMP2": ""
            }
          }
        }
            },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[&1].&",
            "*": {
              "*": "[&2].&"
            }
          }
        }
      }
    ]
    

  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"
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search