admin管理员组

文章数量:1336643

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)

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)

Share Improve this question asked Nov 19, 2024 at 15:37 Katja BürgerKatja Bürger 351 silver badge6 bronze badges 1
  • Hi, Katja, is the input fixed, does the position of the object with id = "brand" may vary I mean ? And do there exist only those two objects nested within the properties array ? – Barbaros Özhan Commented Nov 19, 2024 at 15:49
Add a comment  | 

2 Answers 2

Reset to default 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"
        }
      }
    }
  }
]

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
        }
      ]
    }

本文标签: joltManipulate objects within a jsonStack Overflow