我有以下JSON:
{ "firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": "Maria"
}
我正在尝试获取用户拥有的所有数字的数组或表格,有些用户只有1个号码,有些用户有很多,有没有办法调用所有"数字"在json内
答案 0 :(得分:1)
我打算在手机上回答这个问题,请原谅简洁。
SELECT user->>'firstName' as first_name
, user->>'lastName' as last_name
, phone_type
, phone_number
FROM users_tbl
CROSS JOIN LATERAL jsonb_array_elements(user->'phoneNumbers') jarr(num_obj)
CROSS JOIN LATERAL (
SELECT num_obj->>'type' AS phone_type
, num_obj->>'number' AS phone_number
) nums