将字符串数组转换为单独的对象

huangapple go评论123阅读模式
英文:

Jolt: Convert string array into individual objects

问题

以下是您要翻译的内容:

"I'm trying to convert an array of strings into objects. Which is not workings as I expected. Can someone help me with this?"

"I got struck by splitting the array of items into individual objects. Some of the items are forming as an array only."

Input JSON:

{
  "GroupMembers": {
    "group1": [
      "member1",
      "member2",
      "member3"
    ],
    "group2": [
      "member1",
      "member4",
      "member6",
      "member8"
    ],
    "group3": [
      "member2",
      "member6"
    ]
  }
}

My Spec File:

[
  {
    "operation": "shift",
    "spec": {
      "GroupMembers": {
        "*": {
          "*": {
            "@(1,[&])": {
              "*": {
                "$(3)": "GroupMembers[#4].groupId",
                "$": "GroupMembers[#4].memberId"
              }
            }
          }
        }
      }
    }
  }
]

and my expected output is

{
  "GroupMembers": [
    {
      "groupId": "group1",
      "memberId": "member1"
    },
    {
      "groupId": "group1",
      "memberId": "member2"
    },
    {
      "groupId": "group1",
      "memberId": "member3"
    },
    {
      "groupId": "group2",
      "memberId": "member1"
    },
       .....
       .....    
    {
      "groupId": "group3",
      "memberId": "member6"
    }
  ]
}
英文:

I'm trying to convert an array of strings into objects. Which is not workings as I expected. Can someone help me with this?

I got struck by splitting the array of items into individual objects. Some of the items are forming as an array only.

Input JSON:

{
  "GroupMembers": {
    "group1": [
      "member1",
      "member2",
      "member3"
    ],
    "group2": [
      "member1",
      "member4",
      "member6",
      "member8"
    ],
    "group3": [
      "member2",
      "member6"
    ]
  }
}

My Spec File:

[
  {
    "operation": "shift",
    "spec": {
      "GroupMembers": {
        "*": {
          "*": {
            "@(1,[&])": {
              "*": {
                "$(3)": "GroupMembers[#4].groupId",
                "$": "GroupMembers[#4].memberId"
              }
            }
          }
        }
      }
    }
  }
]

and my expected output is

{
  "GroupMembers": [
    {
      "groupId": "group1",
      "memberId": "member1"
    },
    {
      "groupId": "group1",
      "memberId": "member2"
    },
    {
      "groupId": "group1",
      "memberId": "member3"
    },
    {
      "groupId": "group2",
      "memberId": "member1"
    },
       .....
       .....    
    {
      "groupId": "group3",
      "memberId": "member6"
    }
  ]
}

答案1

得分: 1

以下是您要翻译的内容:

您可以使用以下转换

[
  { // 从数组键生成对象 vs. 数组组件
    "operation": "shift",
    "spec": {
      "GroupMembers": {
        "*": {
          "*": {
            "$1": "&3.&2.&1.groupId", // 按顶级对象、数组及其索引进行分区
            "@": "&3.&2.&1.memberId"
          }
        }
      }
    }
  },
  { // 收集常见数组节点“GroupMembers”下的每个键值对
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2[]"
        }
      }
    }
  }
]

该网站http://jolt-demo.appspot.com/上的演示如下图所示:

将字符串数组转换为单独的对象

英文:

You can use the following transformation

[
  { // form objects from array keys vs. array components
    "operation": "shift",
    "spec": {
      "GroupMembers": {
        "*": {
          "*": {
            "$1": "&3.&2.&1.groupId", // partition by top level object, array and its indexes
            "@": "&3.&2.&1.memberId"
          }
        }
      }
    }
  },
  { // collect each key-value pairs under the common array node, namely "GroupMembers"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2[]"
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

将字符串数组转换为单独的对象

答案2

得分: 1

你可以考虑使用另一个库 Josson 来进行JSON转换。

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "{\"GroupMembers\":{\"group1\":[\"member1\",\"member2\",\"member3\"],\"group2\":[\"member1\",\"member4\",\"member6\",\"member8\"],\"group3\":[\"member2\",\"member6\"]}}");
JsonNode node = josson.getNode(
    "map(GroupMembers.entries().map(groupId:key, memberId:value).unwind(memberId))");
System.out.println(node.toPrettyString());

输出结果

{
  "GroupMembers" : [ {
    "groupId" : "group1",
    "memberId" : "member1"
  }, {
    "groupId" : "group1",
    "memberId" : "member2"
  }, {
    "groupId" : "group1",
    "memberId" : "member3"
  }, {
    "groupId" : "group2",
    "memberId" : "member1"
  }, {
    "groupId" : "group2",
    "memberId" : "member4"
  }, {
    "groupId" : "group2",
    "memberId" : "member6"
  }, {
    "groupId" : "group2",
    "memberId" : "member8"
  }, {
    "groupId" : "group3",
    "memberId" : "member2"
  }, {
    "groupId" : "group3",
    "memberId" : "member6"
  } ]
}
英文:

You may consider another library Josson to do the JSON transformation.

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "{\"GroupMembers\":{\"group1\":[\"member1\",\"member2\",\"member3\"],\"group2\":[\"member1\",\"member4\",\"member6\",\"member8\"],\"group3\":[\"member2\",\"member6\"]}}");
JsonNode node = josson.getNode(
    "map(GroupMembers.entries().map(groupId:key, memberId:value).unwind(memberId))");
System.out.println(node.toPrettyString());

Output

{
  "GroupMembers" : [ {
    "groupId" : "group1",
    "memberId" : "member1"
  }, {
    "groupId" : "group1",
    "memberId" : "member2"
  }, {
    "groupId" : "group1",
    "memberId" : "member3"
  }, {
    "groupId" : "group2",
    "memberId" : "member1"
  }, {
    "groupId" : "group2",
    "memberId" : "member4"
  }, {
    "groupId" : "group2",
    "memberId" : "member6"
  }, {
    "groupId" : "group2",
    "memberId" : "member8"
  }, {
    "groupId" : "group3",
    "memberId" : "member2"
  }, {
    "groupId" : "group3",
    "memberId" : "member6"
  } ]
}

huangapple
  • 本文由 发表于 2023年8月4日 21:08:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定