How can I access a key value in jsonb postgresql? - postgresql

{
"data": {
"val": "{\"cell_number\": \"123\"}"
}
}
I want to get the value in data -> val -> cell_number i.e '123'. Is there a way to do it in postgresql?

If that is not a typo and you put a stringified json object under the val key, then this will untangle it for you:
with invar as (
select '{
"data": {
"val": "{\"cell_number\": \"123\"}"
}
}'::jsonb as jsonb_col
)
select ((jsonb_col->'data'->>'val')::jsonb)->>'cell_number' from invar;
?column?
----------
123
(1 row)
The first step gets you down to the val key. That result has to be returned as text (hence the ->>) and then cast to jsonb so that cell_number can be dereferenced.

Related

Postgres - jsonb create and update new attribute in column

I have such a column attributes:
{
"a:value": "123",
"a:origin": "abc"
}
I want to create a new attribute which should look like this:
"abcKey:value": {
"value": "123ABC",
"version": "1"
}
So, in the end attributes should look like this:
{
"a:value": "123",
"a:origin": "abc",
"abcKey:value": {
"value": "123ABC",
"version": "1"
}
}
How can I do this?
I tried this
update my_table
set attributes = jsonb_set(attributes, '{abcKey:value,value}', '"123ABC"')
attributes = jsonb_set(attributes, '{abcKey:value,version}', '"1"')
where ...;
But this does not work because I think, I have to create the new attribute at first. How can I create and update this new attribute (maybe in one step)?
Thank you very much!
I wrote two samples for you:
-- if you have same objects are json
with tb as (
select
'{
"a:value": "123",
"a:origin": "abc"
}'::jsonb a1,
'{"abcKey:value": {
"value": "123ABC",
"version": "1"
}}'::jsonb a2
)
select a1, a2, a1||a2 from tb; -- you can concate json objects
-- if you can create json objects via using key value
with tb as (
select
'{
"a:value": "123",
"a:origin": "abc"
}'::jsonb a1
)
select a1 || jsonb_build_object('abcKey:value', jsonb_build_object('value', '123ABC', 'version', 1)) from tb;

Sort by json element at nested level for jsonb data - postgresql

I have below table in postgresql which stored JSON data in jsonb type of column.
CREATE TABLE "Trial" (
id SERIAL PRIMARY KEY,
data jsonb
);
Below is the sample json structure
{
"id": "000000007001593061",
"core": {
"groupCode": "DVL",
"productType": "ZDPS",
"productGroup": "005001000"
},
"plants": [
{
"core": {
"mrpGroup": "ZMTS",
"mrpTypeDesc": "MRP",
"supLeadTime": 777
},
"storageLocation": [
{
"core": {
"storageLocation": "H050"
}
},
{
"core": {
"storageLocation": "H990"
}
},
{
"core": {
"storageLocation": "HM35"
}
}
]
}
],
"discriminator": "Material"
}
These are the scripts for insert json data
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000007001593061","core":{"groupCode":"DVL","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZMTS","mrpTypeDesc":"MRP","supLeadTime":777},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}},{"core":{"storageLocation":"HM35"}}]}],"discriminator":"Material"}' AS JSON))
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000000104107816","core":{"groupCode":"ELC","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZCOM","mrpTypeDesc":"MRP","supLeadTime":28},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}}]}],"discriminator":"Material"}' AS JSON))
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000000104107818","core":{"groupCode":"DVK","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZMTL","mrpTypeDesc":"MRP","supLeadTime":28},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}}]}]}' AS JSON))
If try to sort by at first level then it works
select id,data->'core'->'groupCode'
from "Trial"
order by data->'core'->'groupCode' desc
But when I try to sort by at nested level, below is the script then it doesn't work for me, I'm for sure I'm wrong for this script but don't know what is it ? Need assistant if someone knows how to order by at nested level for JSONB data.
select id,data->'plants'
from sap."Trial"
order by data->'plants'->'core'->'mrpGroup' desc
Need assistance for write a query for order by at nested level for JSONB data.
Below query works for me
SELECT id, data FROM "Trial" ORDER BY jsonb_path_query_array(data, '$.plants[*].core[*].mrpGroup') desc limit 100

Insert new item in JSONB column based on value of other field - postgres

I have the following jsonb structure with many entries in it
[
{
"name":"test",
"features":[
{
"name":"feature1",
"granted":false
},
{
"name":"feature2",
"granted":true
}
]
}...
]
I'd like to add a new entry in the features array when the parent name element has value "test" and feature1 granted is "false".
The idea is to write a flyway script to migrate my data.
I've been battling with jsonb_insert but I can't figure out the path portion of it since I can have potentially many elements in there and I can't just add a given subscript.
End result should be:
[
{
"name":"test",
"features":[
{
"name":"feature1",
"granted":false
},
{
"name":"feature2",
"granted":true
},
{
"name":"newFeature",
"granted":false
}
]
}
]
EDIT1
So far I've attempted:
UPDATE my_table SET modules =
jsonb_insert(my_column, '{features, [0]}', '{"name": "newFeature", "granted": false}')
WHERE my_column ->> 'name' = 'test' AND my_column #> '{"features": [{"name":"feature1", "granted": false}]}';
The statement executes but no updates are actually done.
EDIT2
I modified the query just to test the PATH out to
UPDATE my_table SET modules =
jsonb_insert(my_column, '{0, features, 0}', '{"name": "newFeature", "granted": false}')
WHERE my_column ->> 'name' = 'test' AND my_column #> '{"features": [{"name":"feature1", "granted": false}]}';
However this only always updates the first entry in the array, and the object I need to update is not guaranteed to always be in this position
This should be enough information to complete the query:
Let's create the mock data
create table a (id serial primary key , b jsonb);
insert into a (b)
values ('[
{
"name": "test",
"features": [
{
"name": "feature1",
"granted": false
},
{
"name": "feature2",
"granted": true
}
]
},
{
"name": "another-name",
"features": [
{
"name": "feature1",
"granted": false
},
{
"name": "feature2",
"granted": true
}
]
}
]');
Now explode the array using jsonb_array_elements with ordinality to get the index and the property
select first_level.id, position, feature_position, feature
from (select a.id, arr.*
from a,
jsonb_array_elements(a.b) with ordinality arr (elem, position)
where elem ->> 'name' = 'test') first_level,
jsonb_array_elements(first_level.elem -> 'features') with ordinality features (feature, feature_position);
The result of this query is:
1,1,1,"{""name"": ""feature1"", ""granted"": false}"
1,1,2,"{""name"": ""feature2"", ""granted"": true}"
There you have the necessary info that you need to fetch the sub elements that you need, as well as all the indexes that you needed for your query.
Now, to the final edit, you already had the query that you wanted:
UPDATE my_table SET modules =
jsonb_insert(my_column, '{0, features, 0}', '{"name": "newFeature", "granted": false}')
WHERE my_column ->> 'name' = 'test' AND my_column #> '{"features": [{"name":"feature1", "granted": false}]}';
In the where you'll use the id, because those are the rows that you are interested in, and in the indexes you got them from the query. So:
UPDATE my_table SET modules =
jsonb_insert(my_column, '{' || exploded_info.position::string || ', features, ' || exploded_info.feature_position || '}', '{"name": "newFeature", "granted": false}') from (/* previous query */) as exploded_info
WHERE exploded_info.id = my_table.id and exploded_info.feature -> 'granted' = false;
As you can see this easily get's very nasty.
I'd recommend either using a more sql approach, that is, having features in a table instead of inside a json, a fk linking that to your table...
If you really need to use the json, for example, because the domain is really complex and defined at the application level and very flexible. Then I would recommend doing the updates inside app code

Querying Postgres composite types from golang

So I am using go-gorp to query postgres and I can't seem to query the composite type inside my table is giving an error. All I want is appropriately nested JSON response. My postgres schema is:
CREATE TYPE PhoneType AS ENUM ('MOBILE', 'HOME', 'WORK');
CREATE TYPE PhoneNumber AS (
"Number" VARCHAR,
"Type" PhoneType
);
CREATE TABLE Person (
"Id" SERIAL PRIMARY KEY NOT NULL,
"Name" VARCHAR NOT NULL,
"Email" VARCHAR,
"Number" PhoneNumber[]
);
Correspondingly in golang, I have
const (
MOBILE PhoneType = iota
HOME
WORK
)
type PhoneNumber struct {
Number string
Type PhoneType
}
type Person struct {
Id int
Name string
Email string
PhoneNumber // not sure how to get array of phone numbers here
}
And to query, I am using go-gorp as follows:
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
var data []Person
_, err := dbmap.Select(&data, "SELECT * FROM Person")
the result I get has the format
{
"Id":
"Name":
"Email"
"Number": "{\"(..., ...)\"}"
"Type": 0
}
what I'm expecting is:
{
"Id":
"Name":
"Email":
"PhoneNumber": [{
"Number":
"Type":
}]
}
How can I change this nested composite type to the type friendly to my declaration in go?
EDIT: Looks like postres arrays and composite types become strings in go. How can I redesign the schema to achieve similar result?
your type declaration is wrong, try this for example:
type Phone struct {
Number string
Type PhoneType
}
type Person struct {
Id int
Name string
Email string
PhoneNumber []Phone
}

How to pass json array to postgresql function

I have a json object contains array of json.
i.e:
{
ID:'1',
Name:'Pooja',
School:[{Name:'ABC',Address:'Nagpur'},{Name:'CDF'},{Name:'GHI',Year:{From:'2015',To:'2016'}}]
}
I want to insert this in three different table as User, School and Year.
Can anyone help?
Assuming I understood you correctly, you want to split the provided JSON object and write its subobjects to some tables.
PostgreSQL has several JSON operators that might help.
First of all, you should cast JSON's textual representation to type json. This allows you to use JSON operators and functions, such as -> (Get JSON object field):
select
'{
"ID":1,
"Name":"Pooja",
"School":[
{
"Name":"ABC",
"Address":"Nagpur"
},
{
"Name":"CDF"
},
{
"Name":"GHI",
"Year":{
"From":"2015",
"To":"2016"
}
}
]
}'::json -> 'Name';
?column?
----------
Pooja
(1 row)
Or, for example, #> (Get JSON object at specified path):
select 'YOUR_JSON'::json #> '{"School", 2, "Year"}';
?column?
----------------------------
{ +
"From":"2015",+
"To":"2016" +
}
(1 row)
All you have to do now is insert the result of the operator application into the table of your choice:
insert into user select 'YOUR_JSON'::json -> 'Name';
If you simply want to extract the School array, you could still use -> operator:
select 'YOUR_JSON'::json -> 'School';
?column?
-----------------------------
[ +
{ +
"Name":"ABC", +
"Address":"Nagpur"+
}, +
{ +
"Name":"CDF" +
}, +
{ +
"Name":"GHI", +
"Year":{ +
"From":"2015", +
"To":"2016" +
} +
} +
]
(1 row)
Read the documentation for more.