cancel
Showing results for 
Search instead for 
Did you mean: 

Create Key Value Pair Array

alex_panganiban
Contributor

Hoping someone can help me out. I have a JSON array. It’s an array of string values and it’s called, washCare. I want to give each value in the array a property name. How would I write the correct mapping to accomplish the following? I thought I could possibly use the map arrow function, but I couldn’t figure out the correct syntax for the expression.

Original Array Format:

“washCare”:
[ “Machine Wash, WARM”, “Hand Wash Only” ]

Desired Result:

“washCare”:
[
{“instruction”: “Machine Wash, WARM”},
{instruction": “Hand Wash Only”}
]

4 REPLIES 4

alex_panganiban
Contributor

Well, I played around. Not the most elegant solution, but this is what I came up with. There has to be a better way, but this worked. If anyone has a better solution, I’d love to see it. See my photo attachment.

image

SpiroTaleski
Valued Contributor

@alex.panganiban.guild

Please check the attached solution:

Test_Community_2021_05_27.slp (4.7 KB)

Hope this will help !

Regards,
Spiro Taleski

alex_panganiban
Contributor

YES! So much more elegant!! Thank you so much. This helps in a big way.

akidave
Employee
Employee

There is an experimental API here. Given one or more input documents and the same number of desired output documents, the API tries to generate an expression that does the desired transformation.

In this case, change the input to

[
  {
    "washCare": [
      "Machine Wash, WARM",
      "Hand Wash Only"
    ]
  }
]

and output to

[
  {
    "washCare": [
      {
        "instruction": "Machine Wash, WARM"
      },
      {
        "instruction": "Hand Wash Only"
      }
    ]
  }
]

and click on Synthesize. The generated result is

{ washCare: $.washCare.map(elem => { instruction: elem }) }

Note: This is experimental right now and works for limited inputs only