簡體   English   中英

如何使用Jolt替換某些屬性的文本

[英]How to replace text for some properties using jolt

我使用jolt來進行一些轉換,雖然已經可以做到,但是在替換JSON文件某些屬性中的某些文本時遇到了一些問題。

我試圖用/分割值,然后用額外的文本連接我需要的部分,不幸的是,這沒有用。

輸入JSON

{
"components" : {
  "values" : {
    "value1" : {
       "$path" : "1/2/3/bear"
    },
     "value2" : {
       "$path" : "1/2/3/cat"
    },
    "value3" : {
       "$path" : "1/2/3/lion"
    }
  }
}

}

我想使用它的最后一部分在每個$ path值中添加'#/ myvalue /'。

預期結果

{
"components" : {
  "values" :
    "value1" : {
       "$path" : "#/myvalue/bear"
    },
     "value2" : {
       "$path" : "#/myvalue/cat"
    },
    "value3" : {
       "$path" : "#/myvalue/lion"
    }
 }
}

}

為了獲得價值,我嘗試了以下一種方法,但效果並不理想。

[
 {
    "operation": "shift",
    "spec": {
        "components": {
            "values": {
                "*": {
                    "\\$path": { //This key has de $ sign
                        "*/*/*/*": {
                            "$(0,4)": "\\$path" //I need to take the four part and assign that to the \\path value
                        }
                    }
                }
            }
        }
    }
}, {
    "operation": "modify-overwrite-beta",
    "spec": {
        "components": {
            "values": {
                "*": {
                    "\\$path": "=concat('#/myvalue/', @(1,\\$path))"
                }
            }
        }
    }
}

]

您所需要做的就是使用jolt提供的某些功能來操縱原始路徑字符串(例如split,last element,concat等)

該規范將達到目的:

 [{
   "operation": "modify-overwrite-beta",
   "spec": {
     "components": {
       "values": {
         "*": {
           "temp": "=split('/',@(1,\\$path))",
           "last_element": "=lastElement(@(1,temp))",
           "\\$path": "=concat(#/myvalue/,@(1,last_element))"
         }
       }
     }
   }
}, {
   "operation": "shift",
   "spec": {
     "components": {
       "values": {
         "*": {
           "\\$path": "components.values.&1.&"
         }
       }
     }
   }
}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM