M
M
McMike2017-04-25 08:51:11
PostgreSQL
McMike, 2017-04-25 08:51:11

How to select data from a field in json format in Postgres?

Let's say there is a table with columns id, data.
Data contains an array in json format, which has a key, for example name.
How to write a query that would select id and name as a result?
In general, the specific task is to take the sum of the parameter from the json array for a certain sample.
Those. there is a table
id - date - data
1 - 01/01/2017 - {cost:2, name:"x"}
2 - 01/01/2017 - {cost:3, name:"x1"}
4 - 01/02/2017 - {cost: 4, name:"x5"}
For example, you need to take the amount of cost for date =
01/01/2017 query result
date - sum
01.01.2017 - 5
Is it possible to write such a query in Postgres at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2017-04-25
@freeExec

SELECT json->'name' AS name, json->'cost' AS cost 
FROM (SELECT '{"cost":4, "name":"x5"}'::json) AS j

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question