您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么使用jolt json”,在日常操作中,相信很多人在怎么使用jolt json問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用jolt json”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
shift 清空后輸出
default 直接輸出,類似于增量
* 匹配所有 & 取key值 出現在value里 例子:&=&0當前層級 &1向上1級 value里沒有能力獲取key值但是可以通過*,$配合使用 $ 取key值 出現在key里 同上 @ 取value值 出現在key里 例子:@(3,clientId) @取value值 {{{從上往下3級,到clientId,hidden,取clientId的值 # 固定輸出值 出現在key里或value里
josn input
{ "rating": { "primary": { "value": 3 }, "quality": { "value": 3 } } }
json spec
[ { //shfit清空后輸出 "operation": "shift", "spec": { "rating": { "primary": { // 獲取rating.primary.value值 重命名為Rating "value": "Rating" }, // *表示匹配所有json數據,*優先級最低,卻明確越優先,所有下面不會處理primary "*": { // &1 means, go up one level and grab that value and substitute it in // &1表示,向上一級,獲取那個值并代入 // 下面這個場景 &1 = "quality" //SecondaryRatings就是命名的一個固定的字段而已,Value也是 //SecondaryRatings.quality.Value=3 "value": "SecondaryRatings.&1.Value", // "$"表示把input中json的key拿過來 "$": "SecondaryRatings.&1.Id" } } } }, { //default直接輸出,類似于增量 "operation": "default", "spec": { "Range": 5, "SecondaryRatings": { "*": { // SecondaryRatings下都會加上ChildRange=5 "ChildRange": 5 } } } } ]
json output
{ "Rating" : 3, "SecondaryRatings" : { "quality" : { "Id" : "quality", "Value" : 3, "ChildRange" : 5 } }, "Range" : 5 }
json input
{ "Rating": 1, "SecondaryRatings": { "Design": 4, "Price": 2, "RatingDimension3": 1 } }
json spec
[ { "operation": "shift", "spec": { //Rating重命名為rating-primary //rating-primary=1 "Rating": "rating-primary", // 將所有二級評級轉換為前綴數據 "SecondaryRatings": { //&表示&0,也就是向上0級,就是當前級 "*": "rating-&" } } } ]
json output
{ "rating-primary" : 1, "rating-Design" : 4, "rating-Price" : 2, "rating-RatingDimension3" : 1 }
json input
{ "rating-primary": 1, "rating-Price": 2, "rating-Design": 4, "rating-RatingDimension3": 1 }
json spec
[ { "operation": "shift", "spec": { // rating-primary重命名為 Rating "rating-primary": "Rating", // 剩下的rating- //SecondaryRatings.是固定值 //"rating-*": "Ratings2.&", //&是獲取當前級key,也就是rating-Price,rating-Design,rating-RatingDimension3 //這時我想去掉key中rating-,變成Price,Design,RatingDimension3 "rating-*": "Ratings3.&(0,1)" } } ]
json output
{ "Rating" : 1, "SecondaryRatings" : { "Price" : 2, "Design" : 4, "RatingDimension3" : 1 } }
json input
{ "rating": { "primary": { "value": 3, "max": 5 }, "quality": { "value": 3, "max": 7 } } }
json spec
[ { "operation": "shift", "spec": { "rating": { //匹配rating下面一級,也就是primary,quality "*": { //輸出成ratingNames的一個數組 //key都是通過$,也就是$0獲取得來, $=primary,quality "$": "ratingNames[]" //$1=rating } } } } ]
json output
{ "ratingNames" : [ "primary", "quality" ] }
json input
{ "ratings": { "primary": 5, "quality": 4, "design": 5 } }
json spec
[ { "operation": "shift", "spec": { "ratings": { "*": { // #2 means go three levels up the tree (count from 0), // and ask the "ratings" node, how many of it's // children have been matched. // // This allows us to put the Name and the Value into // the same object in the Ratings array. //$=$0當前層級primary,quality,design //#2表示向上移動三層(從0開始計數), //Ratings[]定義一個數組 //數組兩個變量Name和Value //@取value "$": "Ratings[#2].Name", "@": "Ratings[#2].Value" //自己推到一下 //"$": "Ratings[]" "Ratings" : [ "primary", "quality", "design" ] //"$": "Ratings[].test" "Ratings" : [ { // "test" : "primary" // }, { // "test" : "quality" // }, { // "test" : "design" // } ] //"$": "Ratings[#].test" { // "Ratings" : [ { // "test" : [ "primary", "quality", "design" ] // } ] // } // //#=#0=#1 //Ratings[#2].test { // "Ratings" : [ { // "test" : "primary" // }, { // "test" : "quality" // }, { // "test" : "design" // } ] // } // "$": "Ratings[#2].Name", //"@": "Ratings[#2].Value" { // "Ratings" : [ { // "Name" : "primary", // "Value" : 5 // }, { // "Name" : "quality", // "Value" : 4 // }, { // "Name" : "design", // "Value" : 5 // } ] // } // "$": "Ratings[].Name", // "@": "Ratings[].Value" { // "Ratings" : [ { // "Name" : "primary" // }, { // "Value" : 5 // }, { // "Name" : "quality" // }, { // "Value" : 4 // }, { // "Name" : "design" // }, { // "Value" : 5 // } ] // } // "$": "Ratings[#].Name", // "@": "Ratings[#].Value" { // "Ratings" : [ { // "Name" : [ "primary", "quality", "design" ], // "Value" : [ 5, 4, 5 ] // } ] // } //#=#0=#1 } } } } ]
json output
{ "Ratings" : [ { "Name" : "primary", "Value" : 5 }, { "Name" : "quality", "Value" : 4 }, { "Name" : "design", "Value" : 5 } ] }
json input
{ "Photos": [ { "Id": "327703", "Caption": "TEST>> photo 1", "Url": "https://cache.yisu.com/upload/information/20210524/347/779658.jpg" }, { "Id": "327704", "Caption": "TEST>> photo 2", "Url": "https://cache.yisu.com/upload/information/20210524/347/779659.jpg" } ] }
json spec
[ // 平鋪 { "operation": "shift", "spec": { "Photos": { "*": { //&1 取到的是數組的下標 "Id": "photo-&1-id", "Caption": "photo-&1-caption", "Url": "photo-&1-url" } } } } ]
json output
{ "photo-0-id" : "327703", "photo-0-caption" : "TEST>> photo 1", "photo-0-url" : "https://cache.yisu.com/upload/information/20210524/347/779658.jpg", "photo-1-id" : "327704", "photo-1-caption" : "TEST>> photo 2", "photo-1-url" : "https://cache.yisu.com/upload/information/20210524/347/779659.jpg" }
json input
{ "data" : { "1234": { "clientId": "12", "hidden": true }, "1235": { "clientId": "35", "hidden": false } } }
json spec
[ { "operation": "shift", "spec": { "data": { "*": { "hidden": { //value也可以寫在這里的 "true": { //clients固定 //@(3,clientId) @取value值 *所以從data開始 data.xxx.clientId從上往下3級,到clientId,hidden,取clientId的值 //#有點像轉譯符,讓disabled原樣輸出 "#disabled": "clients.@(3,clientId)" }, "false": { "#enabled": "clients.@(3,clientId)" } } } } } } ]
json output
{ "clients" : { "12" : "disabled", "35" : "enabled" } }
json input
{ "data": { "clientId": "1234", "clientName": "Acme" } }
json spec
[ { "operation": "shift", "spec": { "data": { // @clientName取clientName的值Acme // @clientId取clientId的值1234 "@clientName": "bookMap.@clientId" // 取clientName的值 //第1級取cliendId的值 沒有*所以從data里面開始clientName所以是1 // "clientName": "bookMap.@(1,clientId)" } } } ]
json output
{ "bookMap" : { "1234" : "Acme" } }
json input
{ "clientsActive": true, "clients": { "Acme": { "clientId": "Acme", "index": 1 }, "Axe": { "clientId": "AXE", "index": 0 } }, "data": { "bookId": null, "bookName": "Enchiridion" } }
json spec
[ {//先放一下,不好理解 "operation": "shift", "spec": { "clientsActive": { "true": { "@(2,clients)": { // Test the ability to continue to match after doing a Transpose "*": { "clientId": "clientIds[@(1,index)]" } }, // Verify that it something does not exist, it does not output a null "@(2,pants)": "pants" }, "data": { // Verify the ability for Transpose to lookup and use a "valid" null, aka one that was in the input data "@bookId": "books.@bookName" } } } } ]
json output
{ "clientIds" : [ "AXE", "Acme" ] }
json input
{ "comment" : "pulled from http://json-ld.org/playground/ example recipe. Also, Mojitos are good.", "@context": { "name": "http://rdf.data-vocabulary.org/#name", "ingredient": "http://rdf.data-vocabulary.org/#ingredients", "yield": "http://rdf.data-vocabulary.org/#yield", "instructions": "http://rdf.data-vocabulary.org/#instructions", "step": { "@id": "http://rdf.data-vocabulary.org/#step", "@type": "xsd:integer" }, "description": "http://rdf.data-vocabulary.org/#description", "xsd": "http://www.w3.org/2001/XMLSchema#" }, "name": "Mojito", "ingredient": [ "12 fresh mint leaves", "1/2 lime, juiced with pulp", "1 tablespoons white sugar", "1 cup ice cubes", "2 fluid ounces white rum", "1/2 cup club soda" ], "yield": "1 cocktail", "instructions": [ { "step": 1, "description": "Crush lime juice, mint and sugar together in glass." }, { "step": 2, "description": "Fill glass to top with ice cubes." }, { "step": 3, "description": "Pour white rum over ice." }, { "step": 4, "description": "Fill the rest of glass with club soda, stir." }, { "step": 5, "description": "Garnish with a lime wedge." } ] }
json spec
[ { "operation": "shift", "spec": { // 因為 Java 所以使用//轉譯 // 這些都要轉譯 . @ $ & \ [ ] "\\@context": { //內層幾種取值的方法 "name": "&1.Name", "ingredient": "&1.Inputs", "yield": "\\@context.Makes", // pass the rest thru //內層原樣輸出 "*": "&1.&" }, //三個字段重命名 "name": "Name", "ingredient": "Inputs", "yield": "Makes", //剩余的原樣輸出 "*": "&" } } ]
json output
{ "comment" : "pulled from http://json-ld.org/playground/ example recipe. Also, Mojitos are good.", "@context" : { "Name" : "http://rdf.data-vocabulary.org/#name", "Inputs" : "http://rdf.data-vocabulary.org/#ingredients", "Makes" : "http://rdf.data-vocabulary.org/#yield", "instructions" : "http://rdf.data-vocabulary.org/#instructions", "step" : { "@id" : "http://rdf.data-vocabulary.org/#step", "@type" : "xsd:integer" }, "description" : "http://rdf.data-vocabulary.org/#description", "xsd" : "http://www.w3.org/2001/XMLSchema#" }, "Name" : "Mojito", "Inputs" : [ "12 fresh mint leaves", "1/2 lime, juiced with pulp", "1 tablespoons white sugar", "1 cup ice cubes", "2 fluid ounces white rum", "1/2 cup club soda" ], "Makes" : "1 cocktail", "instructions" : [ { "step" : 1, "description" : "Crush lime juice, mint and sugar together in glass." }, { "step" : 2, "description" : "Fill glass to top with ice cubes." }, { "step" : 3, "description" : "Pour white rum over ice." }, { "step" : 4, "description" : "Fill the rest of glass with club soda, stir." }, { "step" : 5, "description" : "Garnish with a lime wedge." } ] }
json input
{ "restaurantId": "ZZ4ORJDY3E", "chainId": "RLR932KI", "orderItems": [ { "itemName": "Small Barqs", "quantity": 2 }, { "itemName": "Mozzz", "quantity": 1 } ] }
json spec
[ { "operation": "shift", "spec": { "chainId": "retailer_id", "restaurantId": "store_id", "orderItems": { //遍歷數組 "*": { // value=quantity的值 1,2 // basket_item[]數組輸出 // 數組key=itemName "quantity": "basket_item.[].@(1,itemName)" } } } } ]
json output
{ "retailer_id" : "RLR932KI", "store_id" : "ZZ4ORJDY3E", "basket_item" : [ { "Small Barqs" : 2 }, { "Mozzz" : 1 } ] }
json input
{ "entities": [ { "type": "alpha", "data": "foo" }, { "type": "beta", "data": "bar" }, { "type": "alpha", "data": "zoo" } ] }
json spec
[ { "operation": "shift", "spec": { "entities": { //根據type的值重建數組 "*": "@type[]" } } } ]
json output
{ "alpha" : [ { "type" : "alpha", "data" : "foo" }, { "type" : "alpha", "data" : "zoo" } ], "beta" : [ { "type" : "beta", "data" : "bar" } ] }
json input
{ "books": [ { "title": "foo", "availability": [ "online" ] }, { "title": "bar", "availability": [ "online", "paperback" ] }, { "title": "baz", "availability": [ "paperback" ] } ] }
json spec
[ { "operation": "shift", "spec": { "books": { "*": { "availability": { "*": { // if the value in the array is "paperback" // grab the whole "book object" and write it out to // a PaperBooks array. // The "@3" means go up the tree 4 levels and grab what is there // 4 levels up cause "real" programmers count from 0 ;) //只取有paperback的數據 //數據取@3的數據books.availability.* "paperback": { "@3": "PaperBooks[]" } } } } } } } ]
json output
{ "PaperBooks" : [ { "title" : "bar", "availability" : [ "online", "paperback" ] }, { "title" : "baz", "availability" : [ "paperback" ] } ] }
到此,關于“怎么使用jolt json”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。