使用json_extrct在嵌套的json中查找元素

时间:2017-01-16 18:28:22

标签: mysql json

我的json看起来像

[
  {
"processor": "VISA",
"mapping": {
  "VSID": "161521582"
 }
 },
{
"processor": "MC",
"mapping": {
  "authAcquirerMID": "720000046943001",
  "authAcquirerICA": "3286"
}
},
{
"processor": "MC",
"mapping": {
  "LocationId": "305901891"
}
},
{
"processor": "VISA",
"mapping": {
  "VSID": "72002961"
}
]

我必须为“处理器”:“VISA”获得VSID的价值?我该怎么做? 现在我试过了

SELECT JSON_EXTRACT('[{"processor":"VISA","mapping":{"VSID":"161521582"}},
{"processor":"MC","mapping":{"authAcquirerMID":"720000046943001","authAcquirerICA":"3286"}},
{"processor":"MC","mapping":{"LocationId":"305901891"}},
{"processor":"MC","mapping":{"authAcquirerMID":"720000046943001","authAcquirerICA":"003286"}},
{"processor":"VISA","mapping":{"VSID":"72002961"}},
{"processor":"AMEX","mapping":{"targetingId":"1045006731"}}]',
'$.mapping.VSID');

但它没有给出任何结果。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您必须使用通配符运算符才能在所有数组中找到它,例如

'$[*].mapping.VSID'

<强>样品

SELECT JSON_UNQUOTE(JSON_EXTRACT(
'[{"processor":"VISA","mapping":{"VSID":"161521582"}},
{"processor":"MC","mapping":{"authAcquirerMID":"720000046943001","authAcquirerICA":"3286"}},
{"processor":"MC","mapping":{"LocationId":"305901891"}},
{"processor":"MC","mapping":{"authAcquirerMID":"720000046943001","authAcquirerICA":"003286"}},
{"processor":"VISA","mapping":{"VSID":"72002961"}},
{"processor":"AMEX","mapping":{"targetingId":"1045006731"}}]'
,'$[*].mapping.VSID')) as result;

["161521582", "72002961"]