Create Key Value Pair Array

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

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.

@alexpanganiban

Please check the attached solution:

Test_Community_2021_05_27.slp (4.7 KB)

Hope this will help !

Regards,
Spiro Taleski

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

1 Like

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

1 Like