从具有多层嵌套数组的输入创建列表

Creating list from input with multiple layers of nested arrays

提问人:Cablesister 提问时间:11/23/2022 最后编辑:Barbaros ÖzhanCablesister 更新时间:2/12/2023 访问量:52

问:

我的输入有多层嵌套数组,我需要从中连接这些字段:、、以形成注释文本。employeeNamesubjecttext

然后,我需要注释文本的类型并创建一个输出,该输出是具有多个对象的单个数组数组,其中包含分组的键值对。我的规范是生成一个数组,其中一个对象包含一个具有许多成员的数组。label

这是我输入的表示:

{
  "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"
        }
      ]
    }
  ]
}

这是我的规格

[
  {
    "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"
                }
              }
            }
          }
        }
      }
    }
    }
  ]

这是我当前的输出:

{
  "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" ]
      }
    ]
  }
}

这是我想要的输出:

{
  "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"
      }
    ]
  }
}

注意:我的输入可以有一个或多个帐户对象。我发现如果只有一个帐户对象,我的规范就可以工作

JSON 转换 嵌套列表 震动

评论

1赞 Mohammadreza Khedri 11/23/2022
我认为您想要的输出是错误的。您有 3 个对象,2 个对象。因此,输出中应该有 6 个对象。commentsaccounts

答:

0赞 Mohammadreza Khedri 11/23/2022 #1

您可以使用以下规范:

[
  {
    "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"
          }
        }
      }
    }
  }
]



0赞 Barbaros Özhan 11/24/2022 #2

如果数组中组件的顺序不重要,则可以在 modify 变换规范中使用连接函数,在确定每个组件的单个数组后,在移位变换规范中,例如"CommentText"textemployeeNamesubject

[
  {
    // 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"
      }
    }
  }
]

如果上述顺序很重要,则可以使用以下规范

[
  {
    // 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"
      }
    }
  }
]