jsonb

How to use postgresql any with jsonb data

有些话、适合烂在心里 提交于 2019-12-11 01:00:31
问题 Related see this question Question I have a postgresql table that has a column of type jsonb. the json data looks like this { "personal":{ "gender":"male", "contact":{ "home":{ "email":"ceo@home.me", "phone_number":"5551234" }, "work":{ "email":"ceo@work.id", "phone_number":"5551111" } }, .. "nationality":"Martian", .. }, "employment":{ "title":"Chief Executive Officer", "benefits":[ "Insurance A", "Company Car" ], .. } } This query works perfectly well select employees->'personal'->'contact'

Jooq custom binding registration in Java

折月煮酒 提交于 2019-12-10 22:16:38
问题 I have a custom binding written to convert from custom type to Postgres json type. This part of the documentation mentions how to register using xml but I'm using Java. I have tried to search to find how to do that but in vain. Any help is appreciated. 回答1: If by "I'm using Java", you mean using programmatic code generator configuration using Java: There's a manual section about programmatic code generator configuration here: http://www.jooq.org/doc/latest/manual/code-generation/codegen

Postgres JSONB: where clause for arrays of arrays

非 Y 不嫁゛ 提交于 2019-12-10 20:12:30
问题 there is in postgres (v 9.5, if it is matter): create table json_test( id varchar NOT NULL, data jsonb NOT NULL, PRIMARY KEY(id) ); Where data is json and contains array of arrays { "attribute": "0", "array1": [{ "id": "a12", "attribute": "1", "array2": [{ "id": "a21", "attribute": "21" }] }, { "id": "a12", "attribute": "2", "array2": [{ "id": "22", "attribute": "22" }] }] } Required: select id from json_test where json_test->>'attribute'='0' and array1.[id='a12'].array2.attribute='22' Query

postgresql - query to build up json

心不动则不痛 提交于 2019-12-10 19:26:10
问题 Running: PostgreSQL 9.6.2 I have data stored in a table that is in the form of a key/value pair. The "key" is actually the path of a json object, each one being a property. So for example if the key was "cogs","props1","value", then the json object would be like so: { "cogs":{ "props1": { "value": 100 } } } I'd like to somehow reconstruct a json object via a SQL query if possible. Here is the test data set: drop table if exists test_table; CREATE TABLE test_table ( id serial, file_id integer

querying JSONB with array fields

房东的猫 提交于 2019-12-10 17:52:34
问题 If I have a jsonb column called value with fields such as: {"id": "5e367554-bf4e-4057-8089-a3a43c9470c0", "tags": ["principal", "reversal", "interest"],,, etc} how would I find all the records containing given tags, e.g: if given: ["reversal", "interest"] it should find all records with either "reversal" or "interest" or both. My experimentation got me to this abomination so far: select value from account_balance_updated where value @> '{}' :: jsonb and value->>'tags' LIKE '%"principal"%'; of

How do I migrate an ActiveRecord model attribute from json to jsonb?

↘锁芯ラ 提交于 2019-12-10 17:48:34
问题 What should the migration look like? I would like to take advantage of the jsonb array querying technique. 回答1: I would write the migration this way: def change reversible do |dir| dir.up { change_column :models, :attribute, 'jsonb USING CAST(attribute AS jsonb)' } dir.down { change_column :models, :attribute, 'json USING CAST(attribute AS json)' } end end I don't know how this compares performance-wise to other solutions, but I tested this on a table with 120,000 records, each record having

Indexing a jsonb array in Postgres

淺唱寂寞╮ 提交于 2019-12-10 17:27:04
问题 I try to set up a GIN index but I do not think my index is used when I run the request, whether I use an operator or a function. Environment In our table we have a JSONB field ( json_aip ) containing a Json that looks like that: { "properties": { "pdi": { "contextInformation": { "tags": ["SOME_TAG"] }, }, } Table creation : create table t_aip ( json_aip jsonb, [...] ); CREATE INDEX idx_aip_tags ON t_aip USING gin ((json_aip -> 'properties' -> 'pdi' -> 'contextInformation' -> 'tags'));

Postgresql: Query Between time range using jsonb field

爷,独闯天下 提交于 2019-12-10 15:24:17
问题 I have a table with two fields: id(serial), data(jsonb) And into data I have records with a Datetime field stored as UNIX timestamps: {"Device":132,"Datetime": 1434166552,...} I'm trying to query between ranges: SELECT * FROM trips WHERE data->>'Datetime' BETWEEN EXTRACT(EPOCH FROM date '2014-04-01') AND EXTRACT(EPOCH FROM date '2014-04-15' + interval '1 day') AND id = 123 Message ERROR: operator does not exist: text >= double precision LINE 3: WHERE data->>'Datetime' BETWEEN Something I'm

How to update a jsonb column's field in PostgreSQL?

ε祈祈猫儿з 提交于 2019-12-10 14:05:21
问题 So I wanted to try jsonb of PostgreSQL. In my table, I have a column called extras of jsonb type. Sample data in extras looks like {"param1": 10, "param2": 15} I would like to modify the JSON using sql statements only. I want to do something like this: Update param1 of extras field by adding 10 to its value if param2 of extras exceeds 12. How can I write a SQL statement like this? I know I can easily do this in the application layer but I would like to do this in the SQL layer itself as the

Postgres GROUP BY on jsonb inner field

不羁的心 提交于 2019-12-10 02:12:05
问题 I am using Postgresql 9.4 and have a table test , with id::int and content::jsonb , as follows: id | content ----+----------------- 1 | {"a": {"b": 1}} 2 | {"a": {"b": 1}} 3 | {"a": {"b": 2}} 4 | {"a": {"c": 1}} How do I GROUP BY on an inner field in the content column and return each group as an array? Specifically, the results I am looking for are: content --------------------------------- [{"a": {"b": 1}},{"a": {"b": 1}}] [{"a": {"b": 2}}] (2 rows) Trying: SELECT json_agg(content) as