Useful shortcuts for vi editor

Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Thursday 26 April 2018

e.g. fetching JSON data

Preparation of Test Data
test=# 
create table T_JSON_DATA (id INTEGER, body JSON);

test=#
insert into T_JSON_DATA
values (1, '{ "name":"John", "age":30, "city":"New York" }');

test=#
insert into T_JSON_DATA
values (2, '{ "name":"Alex", "age":25, "city":"China" }');

test=#
insert into T_JSON_DATA
values (3, '{ "name":"Guru", "age":21, "city":"Russia" }');

test=#
insert into T_JSON_DATA
values (4, '{ "name":"Maximus", "age":25, "city":"Somewhere" }');

test=# commit;

Examples for populating test data
test=#
select * from T_JSON_DATA;

-- to retrieve specific field data
test=#
select body->>'name' from T_JSON_DATA;

-- with aliases
test=#
select body->'name' AS NAME, body->'city' AS CITY from T_JSON_DATA;

-- use with where clause
test=#
select body->'name' AS NAME, body->'city' AS CITY from T_JSON_DATA where id = 1;