2017-02-15 126 views
0

我有以下JSON代碼。我使用OPENJSON將這些值拉入SQL數據庫,但是我遇到了退款對象的路徑問題。使用tSQL遍歷JSON對象 - OPENJSON

我想拉「交易」對象中的「金額」值(所以期望的值應該是298.47)。

SQL代碼(目前只返回空值)

OPEN(json) 
WITH( 
    OtherJSONstuff varchar '$otherjsonstuff' 
    Refund   int  '$.refund[0].transactions.amount' <what should this be 

JSON代碼

"otherjsonstuff": othervalues 
"otherjsonstuff": othervalues 
"object": [ 
    { 
     "id": 212, 
    "items": [ 
     { 
      "id": 151, 
      "quantity": 3, 
      "item_id": 926, 
      "subtotal": 30.0, 
      "tax": 0.0, 
      "item": { 
      "id": 926, 
      "quantity": 3, 
      "price": "10.00", 
      "product_id": 934, 
      "properties": [], 
      "discount": "0.00", 
      "tax": [] 
      } 
     } 
     ], 
     "action": [ 
     { 
      "id": 537, 
      "amount": "298.47", --this is the line I need 
      "kind": "refund", 
      "created": "2016-12-13", 
      "location_id": null, 
      "parent_id": 537, 
     } 
     ], 
    } 
    ], 
+0

您可以添加更多的信息,如預期的結果和實際結果。還請添加您的SQLserver版本 – TheGameiswar

+0

完成,我真的不知道SQLServer版本。無論最新的版本是什麼。儘管如此,我認爲這不會產生很大的影響。 – james5

+0

因爲'transactions'也是一個數組,所以你必須在你的'refund'數組中指定你想要的'transaction'。也許你的路徑應該是'$ .refund [0] .transactions [0] .amount' – JNevill

回答

0

已經格式化的JSON代碼,

它應該是$ .refund [0] .transactions [0] .amount

取決於你想訪問的數組對象,只需增加和dec重新估價。根本原因是瞭解JSON層次結構,JNevil爲您提供了一些很好的資源