英文:
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"
} ]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论