英文:
Jolt Transform an Array Objects and Move Some Fields to Nested Array
问题
I want to transform the following array of diagnoses
and move all the fields except sequence
to a nested coding
array.
JSON Input
{
"diagnoses": [
{
"version": "20230607",
"code": "A100",
"display": "Diagnosis 1",
"sequence": 1
},
{
"version": "20230607",
"code": "B100",
"display": "Diagnosis 2",
"sequence": 2
}
]
}
Desired Output
{
"diagnoses": [
{
"coding": [
{
"version": "20230607",
"code": "A100",
"display": "Diagnosis 1"
}
],
"sequence": 1
},
{
"coding": [
{
"version": "20230607",
"code": "B100",
"display": "Diagnosis 2"
}
],
"sequence": 2
}
]
}
I can transform a single diagnoses
object as expected but can't correctly transpose the array. Here's an example of the input and spec for transforming a single diagnoses
object.
JSON Input (single object)
{
"version": "20230607",
"code": "A100",
"display": "Diagnosis 1",
"sequence": 1
}
Jolt Spec
[
{
"operation": "shift",
"spec": {
"sequence": "&",
"*": "coding[0].&"
}
}
]
英文:
I want to transform the following array of diagnoses
and move all the fields except sequence
to a nested coding
array.
JSON Input
{
"diagnoses": [
{
"version": "20230607",
"code": "A100",
"display": "Diagnosis 1",
"sequence": 1
},
{
"version": "20230607",
"code": "B100",
"display": "Diagnosis 2",
"sequence": 2
}
]
}
Desired Output
{
"diagnoses": [
{
"coding": [
{
"version": "20230607",
"code": "A100",
"display": "Diagnosis 1"
}
],
"sequence": 1
},
{
"coding": [
{
"version": "20230607",
"code": "B100",
"display": "Diagnosis 2"
}
],
"sequence": 2
}
]
}
I can transform a single diagnoses
object as expected but can't correctly transpose the array. Here's an example of the input and spec for transforming a single diagnoses
object.
JSON Input (single object)
{
"version": "20230607",
"code": "A100",
"display": "Diagnosis 1",
"sequence": 1
}
Jolt Spec
[
{
"operation": "shift",
"spec": {
"sequence": "&",
"*": "coding[0].&"
}
}
]
答案1
得分: 1
请忽略你有两种不同类型的输入,并编写自己的 Jolt 规范。因为你的数组输入在 sequence
键中,或者你将它们放在根对象中。
[
{
"operation": "shift",
"spec": {
"diagnoses": {
"*": {
"*": "&2[&1].coding[0].&",
"sequence": "&2[&1].&"
}
},
"*": "coding[0].&",
"sequence": "&"
}
}
]
英文:
Ignore that you have 2 different types of input and write your own Jolt spec. Because you have your array input in the sequence
key, or otherwise you have them in the root object.
[
{
"operation": "shift",
"spec": {
"diagnoses": {
"*": {
"*": "&2[&1].coding[0].&",
"sequence": "&2[&1].&"
}
},
"*": "coding[0].&",
"sequence": "&"
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论