Home > database >  Creating list from input with multiple layers of nested arrays
Creating list from input with multiple layers of nested arrays

Time:11-24

My input has multiple layers of nested arrays from which I need to concatenate these fields: employeeName, subject, text to form comment text.

I then need to label the type of comment text and create an output that is a single array array with multiple objects, that contain grouped key value pairs. My spec is generating an array, with one object that contains an array with many members.

Here is a representation of my input:

{
  "accounts": [
    {
      "comments": [
        {
          "outgetcommentstext": [
            {
              "text": "accountObject1 comment text1"
            }
          ],
          "employeeName": "John Doe",
          "subject": "acct1-obj1-subject"
        },
        {
          "outgetcommentstext": [
            {
              "text": "accountObject1 comment text2"
            }
          ],
          "employeeName": "Jane Doe",
          "subject": "acct1-obj2-subject"
        },
        {
          "outgetcommentstext": [
            {
              "text": "accountObject1 comment text3"
            }
          ],
          "employeeName": "Jax Doe",
          "subject": "acct1-obj3-subject"
        }
      ]
    },
    {
      "comments": [
        {
          "outgetcommentstext": [
            {
              "text": "account2-Object1 comment text1"
            }
          ],
          "employeeName": "Jill Doe",
          "subject": "acct2-obj1-subject"
        },
        {
          "outgetcommentstext": [
            {
              "text": "account2-Object2 comment text2"
            }
          ],
          "employeeName": "Janet Doe",
          "subject": "acct2-obj2-subject"
        },
        {
          "outgetcommentstext": [
            {
              "text": "account2Object3 comment text3"
            }
          ],
          "employeeName": "Jacob Doe",
          "subject": "acct2-obj3-subject"
        }
      ]
    }
  ]
}

Here is my spec

[
  {
    "spec": {
      "accounts": {
        "*": {
          "comments": {
            "*": {
              "outgetcommentstext": {
                "*": {
                  "CommentText": "=concat(@(3,employeeName),'-',@(3,subject),'-',@(1,text))"
                }
              }
            }
          }
        }
      }
    },
    "operation": "modify-overwrite-beta"
  },
  {
    "operation": "shift",
    "spec": {
      "accounts": {
        "*": {
          "comments": {
            "*": {
              "outgetcommentstext": {
                "*": {
                  "CommentText": "Job.JobCommentList[&3].CommentText",
                  "#XYZ": "Job.JobCommentList[&3].CommentType"
                }
              }
            }
          }
        }
      }
    }
    }
  ]

Here is my current output:

{
  "Job" : {
    "JobCommentList" : [ {
      "CommentText" : [ "John Doe-acct1-obj1-subject-accountObject1 comment text1", "Jill Doe-acct2-obj1-subject-account2-Object1 comment text1" ],
      "CommentType" : [ "XYZ", "XYZ" ]
    }, {
      "CommentText" : [ "Jane Doe-acct1-obj2-subject-accountObject1 comment text2", "Janet Doe-acct2-obj2-subject-account2-Object2 comment text2" ],
      "CommentType" : [ "XYZ", "XYZ" ]
    }, {
      "CommentText" : [ "Jax Doe-acct1-obj3-subject-accountObject1 comment text3", "Jacob Doe-acct2-obj3-subject-account2Object3 comment text3" ],
      "CommentType" : [ "XYZ", "XYZ" ]
    } ]
  }
}

This is my desired output:

{
    "Job": {
        "JobCommentList": [
            {
                "CommentText": "John Doe-acct1-obj1-subject-accountObject1 comment text1",
                "CommentType": "XYZ"
            },
            {
                "CommentText": "Jill Doe-acct2-obj1-subject-account2-Object1 comment text1",
                "CommentType": "XYZ"
            },
            {
                "CommentText": "Jane Doe-acct1-obj2-subject-accountObject1 comment text2",
                "CommentType": "XYZ"
            },
            {
                "CommentText": "Jacob Doe-acct2-obj3-subject-account2Object3 comment text3",
                "CommentType": "XYZ"
            }
        ]
    }
}

Note: my input could have one or many account objects. I found that my spec works if there is only one account object

CodePudding user response:

You can using this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": ""
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "CommentText": "=concat(@(3,employeeName),'-',@(3,subject),'-',@(1,text))"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "outgetcommentstext": {
          "*": {
            "CommentText": "Job.JobCommentList[&3].&",
            "#XYZ": "Job.JobCommentList[&3].CommentType"
          }
        }
      }
    }
  }
]



CodePudding user response:

If the order of the components within the "CommentText" array is not important, then you can use a join function within a modify transformation spec, after determining the individual arrays for each components text, employeeName, subject within a shift transformation spec such as

[
  {
    // determine the desired attributes ("CommentType" and "CommentText") while keeping them within different objects 
    "operation": "shift",
    "spec": {
      "*": { // the level of "accounts" array
        "*": { // the level of the indexes of the upper array
          "*": { // the level of "comments" array 
            "*": { // the level of the indexes of the upper array
              "*": "&1[&3].CommentText",
              "outget*": { // the level of "outgetcommentstext" array 
                "*": { // the level of the indexes of the upper array                  
                  "*": "&3[&5].CommentText",
                  "#XYZ": "&3[&5].CommentType" // fixed valued attribute
                }
              }
            }
          }
        }
      }
    }
  },
  {
    // combine each "CommentText" attribute values dash separated 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "CommentText": "=join('-',@(1,&))"
        }
      }
    }
  },
  {
    // get rid of the current integer label indexes while converting them to the desired fixed values
    "operation": "shift",
    "spec": {
      "*": {
        "*": "Job.JobCommentList"
      }
    }
  }
]

if the mentioned order matters, then you can use the following spec

[
  {
    // determine the desired attributes ("CommentType" and "CommentText") while keeping them within different objects 
    "operation": "shift",
    "spec": {
      "*": { // the level of "accounts" array
        "*": { // the level of the indexes of the upper array
          "*": { // the level of "comments" array 
            "*": { // the level of the indexes of the upper array
              "*": "&1[&3].&",
              "outget*": { // the level of "outgetcommentstext" array 
                "*": { // the level of the indexes of the upper array                  
                  "*": "&3[&5].&",
                  "#XYZ": "&3[&5].CommentType" // fixed valued attribute
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "sort"
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "CommentType": "&2[&1].&",
          "*": "&2[&1].CommentText"
        }
      }
    }
  },
  {
    // combine each "CommentText" attribute values dash separated 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "CommentText": "=join('-',@(1,&))"
        }
      }
    }
  },
  {
    // get rid of the current integer label indexes while converting them to the desired fixed values
    "operation": "shift",
    "spec": {
      "*": {
        "*": "Job.JobCommentList"
      }
    }
  }
]
  • Related