skip to Main Content

I have the following JSON including a properties array and I would like to transform it with jolt

JSON INPUT

{
  "kind": "product",
  "product": {
    "id": "product.P-L-0368",
    "productNumber": "P-L-0368",
    "name": "myProduct"
  },
  "properties": [
    {
      "values": [
        {
          "value": "222"
        }
      ],
      "id": "purchase_category",
      "type": "string"
    },
    [
      {
        "id": "brand",
        "type": "string",
        "values": [
          {
            "value": "my_brand",
            "language": null,
            "unit_of_measure": null
          }
        ]
      }
    ]
  ]
}

DESIRED OUTPUT

{
  "kind": "product",
  "product": {
    "id": "product.P-L-0368",
    "productNumber": "P-L-0368",
    "name": "myProduct"
  },
  "properties": [
    {
      "values": [
        {
          "value": "222"
        }
      ],
      "id": "purchase_category",
      "type": "string"
    },
    {
      "id": "brand",
      "type": "string",
      "values": [
        {
          "value": "my_brand",
          "language": null,
          "unit_of_measure": null
        }
      ]
    }
  ]
}

The problem is, that I do not know how to get rid of the array bracket within the properties array (surrounding the brand object)

2

Answers


  1. If the input is in fixed style, then you can match the second indexed object from the one deeper level such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&", //the elements other than "properties"
          "properties": {
            "0": "&1",
            "1": {
              "*": "&2"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. As your json.properties is an unindexed array, you could access it like that :

    json.properties[1] (assuming you always want to change the second element).

    so you could just update the json like this :

    json.properties[1] = {
          "id": "brand",
          "type": "string",
          "values": [
            {
              "value": "my_brand",
              "language": null,
              "unit_of_measure": null
            }
          ]
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search