cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Convert array into different format.

mohit3
New Contributor

Hi All,

I want to convert an input array to expected array. Can you please suggest. 

Input Array :

"Customfield": [
{
"fieldname": "RECTY",
"fieldvalue": "S01"
},
{
"fieldname": "LFDNR",
"fieldvalue": "0000004"
},
{
"fieldname": "ARE4",
"fieldvalue": "467Q"
}]

Expected Array :

"Customfield": [
{
"fieldname": "RECTY",
"fieldvalue": "S01",
"fieldname": "LFDNR",
"fieldvalue": "0000004",
"fieldname": "ARE4",
"fieldvalue": "467Q"
}]

Thanks

Mohit

2 REPLIES 2

virender_prajap
New Contributor III

JSON structure cannot have same key repeated multiple times within a object. The expected output structure is not a proper JSON structure.

j_angelevski
Contributor III

Hi @mohit3 ,

If the objects in the array, specifically, the key names are the same in each object, you can't flatten them into a single object because they all have the same key name.

Scenario 1:

If each object is with different key names in the original array, then you can use the following expression:

sl.ensureArray({}.extend(...jsonPath($, "Customfield[*]")))

The expression above uses the spread operator to extract the objects in the array into a single object. We can split the expression into three parts.

Part 1: Spread operator

{ ...jsonPath($, "Customfield[*]")) }

The spread operator is a versatile syntax that allows you to expand elements from one data structure (like an array or an object) into another. You can read more about it on the following link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

By using your input, and then applying the spread operator in the expression, it will extract all objects from the array and make a single object, here's how the output will look like on the first step:

{
  0: {
    fieldname:"RECTY",
    fieldvalue:"S01"
  }
  1: {
    fieldname:"LFDNR",
    fieldvalue:"0000004"
  }
  2: {
    fieldname:"ARE4",
    fieldvalue:"467Q"
  }
}

Part 2: Extend object

{}.extend(...jsonPath($, "Customfield[*]"))

In the previous part, we extracted the objects from the initial array and now created a single object that holds all objects from the original array. You need to use the extend keyword with the spread operator to make a flat object ( because without the extend keyword, your object will have the index as a key for each original object ). You can read more about extend on the following link: https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1439367/Object+Functions+and+Properties#Ob...

Because you now have multiple objects in the original array with the same key name, the output after this step will look like:

{
   fieldname: "ARE4",
   fieldvalue: "467Q"
}

As you can see, we have a flat object now, but the extend keyword overrides the key value pairs with the same name and writes the last objects with the same key name to the output, hence why you now have only one from the three original objects.

Part 3: Ensure output is an array

I have used the sl.ensureArray() expression to turn the object into an array back again.

sl.ensureArray(<object input>)

The final output will be:

[
  {
    fieldname: "ARE4",
    fieldvalue: "467Q"
  }
]

As I mentioned above, this expression will work fine as long as the objects in the array have different key names.

Scenario 3:

If each object is with the same key name in the original array, then you need to add a unique identifier to each key ( this can be the index itself in the array ๐Ÿ˜ž

sl.ensureArray({}.extend(...jsonPath($, "Customfield[*]").map((val, index) => val.mapKeys((v, k) => k + "[" + index + "]"))))

 This expression is similar to the first one, but in addition to the first one, it adds a unique identifier to each of the key names in the original object to make sure that all key names are unique.

This will gives us the following output:

[
  {
    "fieldname[0]": "RECTY",
    "fieldvalue[0]": "S01",
    "fieldname[1]": "LFDNR",
    "fieldvalue[1]": "0000004",
    "fieldname[2]": "ARE4",
    "fieldvalue[2]": "467Q"
  }
]

 As you can see, it has all of the original objects included in the output.