0

Currently I have the following INPUT JSON. The "temp"-Object has been added with the help of a default spec.

{
  "items": [
    {
      "description": "myDescription",
      "attributes": [
        {
          "identifier": "colour",
          "type": "attribute",
          "values": [
            "DE"
          ],
          "hint": "1"
        },
        {
          "identifier": "pin",
          "type": "attribute",
          "values": [
            "4711"
          ],
          "hint": "1"
        },
        {
          "identifier": "price",
          "type": "price",
          "values": [
            "77.27"
          ],
          "hint": "1"
        },
        {
          "identifier": "weight",
          "type": "attribute",
          "values": [
            "0.47"
          ],
          "hint": "1"
        },
        {
          "identifier": "status",
          "type": "attribute",
          "values": [
            "active"
          ],
          "hint": "1"
        }
      ]
    }
  ],
  "temp": {
    "hint": "1",
    "identifier": "additionalDescription",
    "values": [
      {
        "lang": "de",
        "value": "temp"
      },
      {
        "lang": "en",
        "value": "temp"
      },
      {
        "lang": "fr",
        "value": "temp"
      },
      {
        "lang": "it",
        "value": "temp"
      },
      {
        "lang": "es",
        "value": "temp"
      }
    ],
    "type": "attribute"
  }
}

Desired Output:

{
  "items": [
    {
      "description": "myDescription",
      "attributes": [
        {
          "identifier": "colour",
          "type": "attribute",
          "values": [
            "DE"
          ],
          "hint": "1"
        },
        {
          "identifier": "pin",
          "type": "attribute",
          "values": [
            "4711"
          ],
          "hint": "1"
        },
        {
          "identifier": "price",
          "type": "price",
          "values": [
            "77.27"
          ],
          "hint": "1"
        },
        {
          "identifier": "weight",
          "type": "attribute",
          "values": [
            "0.47"
          ],
          "hint": "1"
        },
        {
          "identifier": "status",
          "type": "attribute",
          "values": [
            "active"
          ],
          "hint": "1"
        },
        {
          "hint": "1",
          "identifier": "additionalDescription",
          "values": [
            {
              "lang": "de",
              "value": "myDescription"
            },
            {
              "lang": "en",
              "value": "myDescription"
            },
            {
              "lang": "fr",
              "value": "myDescription"
            },
            {
              "lang": "it",
              "value": "myDescription"
            },
            {
              "lang": "es",
              "value": "myDescription"
            }
          ],
          "type": "attribute"
        }
      ]
    }
  ]
}

The spec that I am looking for should fullfill two requirements:

  1. integrate the "new" object within the attributes array.
  2. replace the value "temp" by the value of the description field, which can be found at the top of the json.

Unfortunately I already struggle with getting the arrays correctly into the output JSON, not to mention the Overwriting of the value "temp"


[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "description": "&2.myDescription",
          "attributes": {
            "@(3,temp)": "&3.&2.&",
            "*": "&3.&2.&"
          }
        }
      }
    }
  }
]

Any help are highly appreciated. Also, I would be happy to receive hints regarding the first requirement as well. I always struggle when building the RHS (right-hand side) whenever arrays are involved.

2
  • Hi, I noticed the "items" is an array of one element but can it be multiple elements with different description\attributes? If that is the case how do you associate items elements with the temp object\s? Commented May 4, 2024 at 13:20
  • Hello Samer, thanks for your Message. Fortunately there is only one description per item. In the meantime I found out, that regarding the integration of the new - object within the attributes array a simple { "operation": "shift", "spec": { "items": "&", "temp": "items[#2].attributes" } } does the trick. I combine this with the solution of Barbaros and the JSON has its desired output. Commented May 4, 2024 at 14:33

1 Answer 1

0

What you need is to bring the desired attributes value after 4 levels traversing the tree starting from the the value attribute's level within a modify transformation spec along with overwrite option such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "temp": {
        "values": {
          "*": {
            "value": "@(4,items[0].description)"
          }
        }
      }
    }
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Barbaros for this helpful solution. I just add a simple { "operation": "shift", "spec": { "items": "&", "temp": "items[#2].attributes" } } and the JSON has its desired output. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.