Marek
Marek

Reputation: 31

jolt concatenate array elements to single object

I started with Jolt, but I can't concatenate the elements of the array to single string,

I have json like this:

{
  "partNb": "1234",
  "partDescriptions": [
    {
      "country": "GB",
      "language": "en",
      "content": "1 tool description in en_GB"
    },
    {
      "country": "GB",
      "language": "en",
      "content": "2 tool description in en_GB"
    }
  ]
}

and with jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "partNb": "id",
      "partDescriptions": {
        "*": {
          "content": "description"
        }
      }
    }
  }
]

For this I have this output:

{
  "id" : "1234",
  "description" : [ "1 tool description in en_GB", "2 tool description in en_GB" ]
}

but how to get result like this?:

{
  "id" : "1234",
  "description" :  "1 tool description in en_GB , 2 tool description in en_GB" 
}

Upvotes: 3

Views: 3585

Answers (2)

Sergey Shcherbakov
Sergey Shcherbakov

Reputation: 4778

To get only content fields concatenated into descriptions:

[
  {
    "operation": "shift",
    "spec": {
      "partDescriptions": {
        "*": {
          "content": {
            "@": "content"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "description": "=join(', ', @(2,content))"
    }
  }
]

Output:

{
  "content" : [ "1 tool description in en_GB", "2 tool description in en_GB" ],
  "description" : "1 tool description in en_GB, 2 tool description in en_GB"
}

Upvotes: 2

Milo S
Milo S

Reputation: 4586

Spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "description": "=join(', ',@(1,description))"
    }
  }
]

Upvotes: 2

Related Questions