英文:
Jolt Array transformation
问题
我的输入 JSON 如下:
{
"primary": 5,
"quality": 4,
"design": 5
}
输出应该是:
{
"Ratings": [
{
"Name": "primary",
"Value": 5
},
{
"Name": "quality",
"Value": 4
}
]
}
也就是说,我不希望将输入的所有字段都放在数组中。
有人能提供如何使用 Jolt 来实现这个需求吗?谢谢。
英文:
My input json is like
{
"primary": 5,
"quality": 4,
"design": 5
}
and output should be
{
"Ratings": [
{
"Name": "primary",
"Value": 5
},
{
"Name": "quality",
"Value": 4
}
]
}
i.e I dont want all fields from input to be in array.
Could any one suggest how to do that using jolt..
Thanks in advance..
答案1
得分: 1
以下是翻译的代码部分:
[
{
"operation": "shift",
"spec": {
"p*|q*": { // 键以 p 或 q 开头的属性
"$": "Ratings[#2].Name", // 通过循环遍历属性生成的索引来数组方式遍历属性的两个级别(`:`和`{`)之后的树路径
"@": "Ratings[#2].Value"
}
}
}
]
[
{
"operation": "shift",
"spec": {
"p*|q*": {
"$": "Ratings[#2].Name",
"@": "Ratings[#2].Value",
"@1,ip": "ip" // 从对象内部调用属性
}
}
},
{
"operation": "cardinality",
"spec": {
"Ratings": "MANY",
"*": "ONE"
}
}
]
英文:
You can use the following shift transformation spec
[
{
"operation": "shift",
"spec": {
"p*|q*": { // the attributes whose keys start with p or q
"$": "Ratings[#2].Name", // array-wise loop through the generated indexes of the attributes after traversing two levels( `:` and `{` ) for the tree path
"@": "Ratings[#2].Value"
}
}
}
]
where $ wildcard represents keys
, and @ wildcard represents values
respectively
the demo on the site http://jolt-demo.appspot.com/ is
Edit : For the new proposed case you might tweak the spec as below
[
{
"operation": "shift",
"spec": {
"p*|q*": {
"$": "Ratings[#2].Name",
"@": "Ratings[#2].Value",
"@1,ip": "ip" // call attribute from the inside of the object
}
}
},
{
"operation": "cardinality",
"spec": {
"Ratings": "MANY",
"*": "ONE"
}
}
]
in order to prevent getting redundant null
components
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论