Insert Json data into temp table using SQL Server Json

huangapple go评论63阅读模式
英文:

Insert Json data into temp table using SQL Server Json

问题

I try to insert below json in temp table,

{
  "offers": {
    "burstableenablement": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement1": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement2": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    }
  }
}

Table:
the table should be like

burstableenablement   | australia-central   | 35.635 | WebDirect
burstableenablement   | australia-central-2 | 35.635 | WebDirect
burstable-enablement1 | australia-central   | 35.635 | WebDirect
burstable-enablement1 | australia-central-2 | 35.635 | WebDirect

the above table shows like in temp, I don't know how to achieve that, any solution is helpful for me and thanks in advance.

英文:

I try to insert below json in temp table,

{
  "offers": {
    "burstableenablement": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement1": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement2": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    }
  }
}

Table:
the table should be like

burstableenablement   | australia-central   | 35.635 | WebDirect
burstableenablement   | australia-central-2 | 35.635 | WebDirect
burstable-enablement1 | australia-central   | 35.635 | WebDirect
burstable-enablement1 | australia-central-2 | 35.635 | WebDirect

the above table show like in temp, i don't know how to achieve that, any solution it helpful for me and thanks in advance.

答案1

得分: 0

以下是您提供的SQL查询的翻译:

选择 x.[key]y.[key]json_value(y.value'$.value') 作为 valuejson_value(y.value'$.pricingType')
 openjson(N'{
  "offers": {
    "burstableenablement": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement1": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement2": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    }
  }
}', '$.offers') x
交叉应用 openjson(value'$.prices') y
英文:

Something like this perhaps:

select x.[key], y.[key], json_value(y.value, '$.value') AS value, json_value(y.value, '$.pricingType')
from openjson(N'{
  "offers": {
    "burstableenablement": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement1": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    },
    "burstable-enablement2": {
      "prices": {
        "australia-central": {
          "value": 35.635,
          "pricingType": "WebDirect"
        },
        "australia-central-2": {
          "value": 35.635,
          "pricingType": "WebDirect"
        }
      }
    }
  }
}', '$.offers') x
cross apply openjson(value, '$.prices') y

huangapple
  • 本文由 发表于 2023年4月17日 20:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定