May*_*nkJ 6 postgresql json yaml ruby-on-rails-4 ruby-on-rails-5
我在 Ubuntu 机器上使用 PostgreSQL 9.5.10。我需要编写一个 SQL 查询来从 JSONB 列中选择数据。但是 JSONB 列包含 YAML 格式的数据作为字符串而不是 JSON 格式。
数据库 JSONB 字段返回以下数据:
"---\nemployee:\n reference: '6'\n first_name: Test\n last_name: Test\n email: test@test.test.edu\n new_title: Manager in Training\n former_title: N/A\n annual_salary: 5000000\n new_salary_grade: '10'\n annual_salary_currency: USD\n former_salary_grade: N/A\norigin_address:\n country_alpha3: USA\n country_name: USA\n"
如果我们有来自 JSONB 列的 JSON 数据,例如:
{"employee": {"reference": "6", "first_name": "Test", "last_name": "Test", "email": "test@test.test.edu", "new_title": "Manager in Training", "former_title": "N/A", "annual_salary": 5000000, "new_salary_grade": "10", "annual_salary_currency": "USD", "former_salary_grade": "N/A"}, "origin_address": {"country_alpha3": "USA", "country_name": "USA"}}
然后我们可以在 JSONB 列上编写 SQL 查询,例如:
select test_field->'employee'->'first_name' as name from employees;
但是当列返回 YAML 字符串时,此查询不适用于 JSONB 列。
我没有找到任何预定义的函数或正则表达式来在 SQL 查询中将 YAML 字符串解析为 JSON 格式。
请帮助我在 JSONB 列上对 YAML 字符串格式数据进行 SQL 查询。
谢谢!
| 归档时间: |
|
| 查看次数: |
1229 次 |
| 最近记录: |