溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用jolt json

發(fā)布時間:2021-11-16 15:54:37 來源:億速云 閱讀:239 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“怎么使用jolt json”,在日常操作中,相信很多人在怎么使用jolt json問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用jolt json”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

模式的含義

  • shift 清空后輸出

  • default 直接輸出,類似于增量

* 匹配所有
& 取key值	    出現(xiàn)在value里 	    例子:&=&0當前層級  &1向上1級		value里沒有能力獲取key值但是可以通過*,$配合使用
$ 取key值	    出現(xiàn)在key里		    同上
@ 取value值	    出現(xiàn)在key里		    例子:@(3,clientId)  @取value值 {{{從上往下3級,到clientId,hidden,取clientId的值
# 固定輸出值	出現(xiàn)在key里或value里

盜夢空間 - Inception

josn input

{
  "rating": {
    "primary": {
      "value": 3
    },
    "quality": {
      "value": 3
    }
  }
}

json spec

[
  {
  //shfit清空后輸出
    "operation": "shift",
    "spec": {
      "rating": {
        "primary": {
          // 獲取rating.primary.value值 重命名為Rating
          "value": "Rating"
        },
        // *表示匹配所有json數(shù)據(jù),*優(yōu)先級最低,卻明確越優(yōu)先,所有下面不會處理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
}

給所有key添加rating-前綴 - Convert nested data, to 'prefix soup'.

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",
      // 將所有二級評級轉(zhuǎn)換為前綴數(shù)據(jù)
      "SecondaryRatings": {
        //&表示&0,也就是向上0級,就是當前級
        "*": "rating-&"
      }
    }
  }
]

json output

{
  "rating-primary" : 1,
  "rating-Design" : 4,
  "rating-Price" : 2,
  "rating-RatingDimension3" : 1
}

一級結(jié)構(gòu)根據(jù)key規(guī)則轉(zhuǎn)換為多級結(jié)構(gòu) - Convert 'prefix soup', to nested data.

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
  }
}

獲取LHS鍵值 - Grab LHS key values.

json input

{
  "rating": {
    "primary": {
      "value": 3,
      "max": 5
    },
    "quality": {
      "value": 3,
      "max": 7
    }
  }
}

json spec

[
  {
    "operation": "shift",
    "spec": {
      "rating": {
      //匹配rating下面一級,也就是primary,quality
        "*": {
          //輸出成ratingNames的一個數(shù)組
          //key都是通過$,也就是$0獲取得來, $=primary,quality
          "$": "ratingNames[]"
          //$1=rating
        }
      }
    }
  }
]

json output

{
  "ratingNames" : [ "primary", "quality" ]
}

Map(key-value)轉(zhuǎn)List(數(shù)組) - Map to list.

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開始計數(shù)),
          //Ratings[]定義一個數(shù)組
          //數(shù)組兩個變量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
  } ]
}

List(array) to Map(key value) - List to Map.

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 取到的是數(shù)組的下標
          "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"
}

對象屬性轉(zhuǎn)map(key value)- On a match, apply a String default.

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的值
              //#有點像轉(zhuǎn)譯符,讓disabled原樣輸出
              "#disabled": "clients.@(3,clientId)"
            },
            "false": {
              "#enabled": "clients.@(3,clientId)"
            }
          }
        }
      }
    }
  }
]

json output

{
  "clients" : {
    "12" : "disabled",
    "35" : "enabled"
  }
}

基本情況簡單轉(zhuǎn)置 - Base case simple Transpose.

json input

{
  "data": {
    "clientId": "1234",
    "clientName": "Acme"
  }
}

json spec

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        // @clientName取clientName的值A(chǔ)cme
        // @clientId取clientId的值1234
        "@clientName": "bookMap.@clientId"
        // 取clientName的值
        //第1級取cliendId的值 沒有*所以從data里面開始clientName所以是1
        // "clientName": "bookMap.@(1,clientId)"
      }
    }
  }
]

json output

{
  "bookMap" : {
    "1234" : "Acme"
  }
}

復(fù)/嵌套轉(zhuǎn)置 - Complex / nested Transpose.

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" ]
}

轉(zhuǎn)譯 - Escaping Shiftr special chars.

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 所以使用//轉(zhuǎn)譯
      // 這些都要轉(zhuǎn)譯 . @ $ & \ [  ]
      "\\@context": {
        //內(nèi)層幾種取值的方法
        "name": "&1.Name",
        "ingredient": "&1.Inputs",
        "yield": "\\@context.Makes",
        // pass the rest thru
        //內(nèi)層原樣輸出
        "*": "&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."
  } ]
}

轉(zhuǎn)置數(shù)組中的數(shù)據(jù) - Transpose data in an Array.

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": {
      //遍歷數(shù)組
        "*": {
          // value=quantity的值  1,2
          // basket_item[]數(shù)組輸出
          // 數(shù)組key=itemName
          "quantity": "basket_item.[].@(1,itemName)"
        }
      }
    }
  }
]

json output

{
  "retailer_id" : "RLR932KI",
  "store_id" : "ZZ4ORJDY3E",
  "basket_item" : [ {
    "Small Barqs" : 2
  }, {
    "Mozzz" : 1
  } ]
}

根據(jù)數(shù)組中某個屬性重建數(shù)組 - Bucket data from an Array, based on a leaf level value.

json input

{
  "entities": [
    {
      "type": "alpha",
      "data": "foo"
    },
    {
      "type": "beta",
      "data": "bar"
    },
    {
      "type": "alpha",
      "data": "zoo"
    }
  ]
}

json spec

[
  {
    "operation": "shift",
    "spec": {
      "entities": {
        //根據(jù)type的值重建數(shù)組
        "*": "@type[]"
      }
    }
  }
]

json output

{
  "alpha" : [ {
    "type" : "alpha",
    "data" : "foo"
  }, {
    "type" : "alpha",
    "data" : "zoo"
  } ],
  "beta" : [ {
    "type" : "beta",
    "data" : "bar"
  } ]
}

取一部分數(shù)據(jù) - Filter data from an Array, based on a leaf level value.

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的數(shù)據(jù)
              //數(shù)據(jù)取@3的數(shù)據(jù)books.availability.*
              "paperback": {
                "@3": "PaperBooks[]"
              }
            }
          }
        }
      }
    }
  }
]

json output

{
  "PaperBooks" : [ {
    "title" : "bar",
    "availability" : [ "online", "paperback" ]
  }, {
    "title" : "baz",
    "availability" : [ "paperback" ]
  } ]
}

到此,關(guān)于“怎么使用jolt json”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI