Home > Software design >  JOLT converting an array to a string with an indifinite numbers of elements in the array
JOLT converting an array to a string with an indifinite numbers of elements in the array

Time:01-27

Help pls. I lookead at all simular questions, but did not find an answer to my. I know how converting this

{
  "rooms": {
    "room_number": [
      {
        "number": "1"
      },
      {
        "number": "2"
      },
      {
        "number": "3"
      },
      {
        "number": "4"
      },
      {
        "number": "5"
      }
    ]
  }
}

into this

{
  "room_numbers" : "1;2;3;4;5"
}

with a this JOLT

[
  {
    "operation": "shift",
    "spec": {
      "rooms": {
        "room_number": {
          "*": {
            "@(number)": "room_numbers[]"
          }
        }
      }
    }
    },
  {
    "operation": "modify-overwrite-beta",
    "spec": { "room_numbers": "=concat(@(2,room_numbers[0]),';',@(2,room_numbers[1]),';',@(2,room_numbers[2]),';',@(2,room_numbers[3]),';',@(2,room_numbers[4]))" }
  }
]

but i d'nt know how this make if number of elements in a array is a variable.

CodePudding user response:

You can use join rather than concat along with modify transformation in order to perform concatenation at once instead of adding each array element individually such as

[
  {
   // dynamically get the array of numbers, namely "room_numbers" 
    "operation": "shift",
    "spec": {
      "rooms": {
        "room_number": {
          "*": {
            "*": "&2s"
          }
        }
      }
    }
  },
  {
    // concatenate all of them
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join(';',@(1,&))"
    }
  }
]

enter image description here

  •  Tags:  
  • Related