<grades><grade>3</grade><grade>4</grade><grade>5</grade></grades>
<grades><grade>3<grade>4<grade>5</grades>
<tag/>
instead of
<tag></tag>
<a><b>text</b></a>
, compare with [(text)]
<a><b>text</a></b>
, compare with [(text])
/
/city
/city/name
/city/university/website
/city//website
{"city": { "name": "Gothenburg", "universities": [{"name": "Chalmers", "website": "www.chalmers.se"}, {"name": "University of Gothenburg", "website": "www.gu.se"} ] } }
7 -3.14 5.3e6
true false
null
"Manhattan"
{}
{city:"Boston", population:700000}
[]
[2,3,5,7,11,13]
[1, "hello", {"start":0}, true, [3,4,5]]
{"city": { "name": "Gothenburg", "universities": [{"name": "Chalmers", "website": "www.chalmers.se"}, {"name": "University of Gothenburg", "website": "www.gu.se"} ] } }
d
d.city.name == "Gothenburg"
d.city.universities[0] == {"name": "Chalmers", "website": "www.chalmers.se"}
d.city.universities[1].website == "www.gu.se"
d.name == null
country | currency | value |
---|---|---|
Finland | EUR | 1.09 |
Estonia | EUR | 1.09 |
Sweden | SEK | 0.12 |
[{"country": "Finland",currency: "EUR", value: 1.09}, {"country": "Estonia",currency: "EUR", value: 1.09}, {"country": "Sweden", currency: "SEK", value: 0.12}]
{ "teacher" : { "firstname": "Thomas", "lastname": "Hallgren", "course": "Databases" } }
JSON
JSONB
CREATE TABLE Users(uname TEXT PRIMARY KEY, email TEXT UNIQUE); CREATE TABLE Posts( id SERIAL PRIMARY KEY, author TEXT NOT NULL REFERENCES Users(uname), created TIMESTAMP NOT NULL, content JSONB NOT NULL );
jsonpostgres.sql
.
INSERT INTO Posts VALUES ( DEFAULT, 'Jonas', CURRENT_TIMESTAMP, '{"link" : "https://xkcd.com/327/", "preview":true}' :: JSONB ); INSERT INTO Posts VALUES ( DEFAULT, 'Jonas', CURRENT_TIMESTAMP, '{"picture" : "funnycat.gif", "prop":{"size":15434}}' :: JSONB );
SELECT * FROM Posts; SELECT id,content->'link' AS Url FROM Posts; SELECT id,content FROM Posts WHERE content->'picture' IS NOT NULL SELECT * FROM Posts WHERE content->'preview' = 'true' SELECT id,content->'prop'->'size' AS postsize FROM Posts
jsonb_build_object
SELECT id, jsonb_build_object( 'postid',id, 'user',author ) AS jsondata FROM Posts;
SELECT jsonb_agg(author) FROM Posts;
||
to join two JSON objects:
SELECT author,jsonb_agg(jsonb_build_object('id',id) || content) FROM Posts GROUP BY author;
jsonpostgres.sql
: example by Jonas Duregård.