create table sort(authorized json);
insert into sort values('{"name":"Authorized","states":[{"state_code":"CT","state_name":"Connecticut","is_checked":false},{"state_code":"NJ","state_name":"New Jersey","is_checked":false},{"state_code":"SC","state_name":"South Carolina","is_checked":false},{"state_code":"FL","state_name":"Florida","is_checked":false},{"state_code":"MT","state_name":"Montana","is_checked":false},{"state_code":"GA","state_name":"Georgia","is_checked":false},
{"state_code":"IA","state_name":"Iowa","is_checked":false},{"state_code":"AR","state_name":"Arkansas","is_checked":false},{"state_code":"UT","state_name":"Utah","is_checked":false},{"state_code":"ID","state_name":"Idaho","is_checked":false},{"state_code":"IL","state_name":"Illinois","is_checked":false},{"state_code":"IN","state_name":"Indiana","is_checked":false},{"state_code":"MA","state_name":"Massachusetts","is_checked":false},{"state_code":"MI","state_name":"Michigan","is_checked":false},{"state_code":"MS","state_name":"Mississippi","is_checked":false},{"state_code":"NM","state_name":"New Mexico","is_checked":false},{"state_code":"NV","state_name":"Nevada","is_checked":false},{"state_code":"RI","state_name":"Rhode Island","is_checked":false},{"state_code":"SD","state_name":"South Dakota","is_checked":false},{"state_code":"UT","state_name":"Utah","is_checked":false},{"state_code":"WV","state_name":"West Virginia","is_checked":false},{"state_code":"SD","state_name":"South Dakota","is_checked":false},
{"state_code":"WV","state_name":"West Virginia","is_checked":false}]}')
I want that this json sort according to state_code after update it looks like
{"name":"Authorized","states":[{"state_code":"AR","state_name":"Arkansas","is_checked":false},{"state_code":"CT","state_name":"Connecticut","is_checked":false},{"state_code":"FL","state_name":"Florida","is_checked":false},{"state_code":"GA","state_name":"Georgia","is_checked":false},
{"state_code":"IA","state_name":"Iowa","is_checked":false},{"state_code":"ID","state_name":"Idaho","is_checked":false},{"state_code":"IL","state_name":"Illinois","is_checked":false},{"state_code":"IN","state_name":"Indiana","is_checked":false},{"state_code":"MA","state_name":"Massachusetts","is_checked":false},{"state_code":"MI","state_name":"Michigan","is_checked":false},{"state_code":"MS","state_name":"Mississippi","is_checked":false},{"state_code":"MT","state_name":"Montana","is_checked":false},{"state_code":"NJ","state_name":"New Jersey","is_checked":false},{"state_code":"NM","state_name":"New Mexico","is_checked":false},{"state_code":"NV","state_name":"Nevada","is_checked":false},{"state_code":"RI","state_name":"Rhode Island","is_checked":false},{"state_code":"SC","state_name":"South Carolina","is_checked":false},{"state_code":"SD","state_name":"South Dakota","is_checked":false},{"state_code":"UT","state_name":"Utah","is_checked":false},{"state_code":"WV","state_name":"West Virginia","is_checked":false},{"state_code":"SD","state_name":"South Dakota","is_checked":false},{"state_code":"UT","state_name":"Utah","is_checked":false},
{"state_code":"WV","state_name":"West Virginia","is_checked":false}]}
try this:
select substr(column_name, position ('state_code":"' in column_name)+13,4) as stateCode
from table_name
order by stateCode asc/desc
note:the value 13 represents the lenght of field (state_code":) and 4 represents value of field ("AR")
might need to tweak a little according to you table and column. The select part would select your desired element then you only have to order it by asc or desc.
You can get the results ordered when retrieving the data through sorting within the subquery while extracting the concerned array(states) as a seperate column, and then aggregating the individual objects of the array back while adhering name element of the object such as
SELECT JSON_AGG(
JSON_BUILD_OBJECT( 'name', name, 'states', states)
) AS authorized
FROM
(
SELECT authorized->'name' AS name, j.states
FROM sort,
JSON_ARRAY_ELEMENTS(authorized->'states') AS j(states)
ORDER BY j.states->>'state_code' ) AS ns
Demo
I have table name players where I store json array into stats column, which is like following
{"sbuilders":{"perfect_builds":2,"rounds_wins":3,"games_played":10},
"ffa":{"uhc":{"deaths":12,"kills":8},
"op":{"kills":5,"deaths":1},
"classic":{"deaths":70,"kills":115}}}
But how can I get top 10 sbuilders game_played from this table?
I haven't worked with JSON in mysql much, so this was interesting.
You can use JSON_EXTRACT to extract a json attribute from a json object; you can then use this result as you desire.
select JSON_EXTRACT(i.j, '$.sbuilders.games_played') as gamesplayed from i;
https://www.db-fiddle.com/f/mNiqfif4si7BMZG4vA5cvJ/0
It seems that you need in
SELECT *
FROM players
ORDER BY JSON_EXTRACT(stats, '$.sbuilders.games_played') DESC
LIMIT 10
My query below does not give me any result
WITH dataset AS (
SELECT responseelements FROM cloudtrail_logs
WHERE useridentity.type = 'Root'
AND eventname='CreateVpc'
ORDER BY eventsource, eventname;
AS blob
)
SELECT
json_extract(blob, '$.vpc.vpcId') AS name,
json_extract(blob, '$.ownerId') AS projects
FROM dataset
But if I run only the inner query
SELECT responseelements FROM cloudtrail_logs
WHERE useridentity.type = 'Root'
AND eventname='CreateVpc'
ORDER BY eventsource, eventname;
it gives me the correct response as a Json
{"requestId":"40aaffac-2c53-419e-a678-926decc48557","vpc":{"vpcId":"vpc-01eff2919c7c1da07","state":"pending","ownerId":"347612567792","cidrBlock":"10.0.0.0/26","cidrBlockAssociationSet":{"items":[{"cidrBlock":"10.0.0.0/26","associationId":"vpc-cidr-assoc-04136293a8ac73600","cidrBlockState":{"state":"associated"}}]},"ipv6CidrBlockAssociationSet":{},"dhcpOptionsId":"dopt-92df95e9","instanceTenancy":"default","tagSet":{},"isDefault":false}}
and if I pass this as data as below
WITH dataset AS (
SELECT '{"requestId":"40aaffac-2c53-419e-a678-926decc48557","vpc":{"vpcId":"vpc-01eff2919c7c1da07","state":"pending","ownerId":"347612567792","cidrBlock":"10.0.0.0/26","cidrBlockAssociationSet":{"items":[{"cidrBlock":"10.0.0.0/26","associationId":"vpc-cidr-assoc-04136293a8ac73600","cidrBlockState":{"state":"associated"}}]},"ipv6CidrBlockAssociationSet":{},"dhcpOptionsId":"dopt-92df95e9","instanceTenancy":"default","tagSet":{},"isDefault":false}}'
AS blob
)
SELECT
json_extract(blob, '$.vpc.vpcId') AS name,
json_extract(blob, '$.ownerId') AS projects
FROM dataset
it gives me result , what I am missing here ? So that I am able to make it run in one shot
Is it at all possible?
You're referencing the wrong column name in your query, it should be json_extract(responseelements, '$.vpc.vpcId') AS name instead of json_extract(blob, '$.vpc.vpcId') AS name. The AS blob part of this query does nothing since you can't alias an entire query, so take it out.
The AS blob works in your last example because you're selecting a value (the json string) into a column and the AS blob gives the column a name or alias of "blob". In your original query, you're selecting an existing column named responseelements so that's what you need to refer to in the json_extract function.
I am using postgres v9.3
I have a table called temp which have a column all_data. The value looks something like below :-
{"Accountid" : "1364", "Personalid" : "4629-87c3-04e6a7a60208", "quote_number" : "QWQA62364384"}
Now, I want to query the all_data column by accountid=1364.
Could you please tell what would be the query?
Use the ->> operator
select *
from temp
where all_data ->> 'Accountid' = '1364';
Online example for 9.3: http://sqlfiddle.com/#!15/55981/1
However the above will not work if the JSON contains an array instead of a JSON object. E.g. a value like '[1,2]'::json will cause that query to fail.
With 9.4 and above you could check that using json_typeof() but with your soon to be unsupported version the only workaround I can think of is to convert the column to text and exclude those that start with a [
with valid_rows as (
select *
from temp
where all_data::text not like '[%'
)
select *
from valid_rows
where all_data ->> 'Accountid' = '1364';
Online example for 9.3: http://sqlfiddle.com/#!15/d01f43/3
I'm trying to extract an ID (5384) that is inside an array contained in JSON using wildcards. The problem I'm having is that the position of the ID doesn't have a fixed position for each element in that array. An example of my JSON in that array is like this (where "id":5384 could occupy different indexed positions):
{
"id":7465115,
"name":"BCA_WS_FBX_Nielsen PRIZM_Test_Unlock_1x1",
"advertiser_id":155085,
"pixels":[
{
"id":416491,
"pixel_template_id":null,
},
{
"id":5384,
"pixel_template_id":null,
}
]
}
My query is as follows:
SELECT id, json FROM PROD_APPNEXUS.dimension_json_creatives
WHERE JSON LIKE ('%pixels%_%"id":5384,%') AND MEMBER_ID = 364
I'm trying to extract only items that are in the pixels array and have an ID of 5384.
Any comments as to how to achieve this would be highly valued, thanks!
UPDATE: MySQL version 5.6.17
Sam
Try:
12.6 The JSON Data Type. MySQL 5.7.8+
SELECT
`id`,
`json` -- Data Type JSON
FROM
`PROD_APPNEXUS`.`dimension_json_creatives`
WHERE
JSON_CONTAINS(`json`, '{"id": 5384}', '$.pixels') AND
`MEMBER_ID` = 364;
UPDATE
Using 5.6.17, one option is:
SELECT
`id`,
`json`
FROM
`PROD_APPNEXUS`.`dimension_json_creatives`
WHERE
`json` REGEXP '"pixels":\\[.*"id":5384.*]' AND
MEMBER_ID = 364;
Performance may be affected depending on the number of tuples in the table.
The only way I would think of is to use the REGEXP syntax of MySQL:
SELECT id, json FROM PROD_APPNEXUS.dimension_json_creatives
WHERE (JSON REGEXP '("pixels":\[)?.*"id":5384') AND MEMBER_ID = 364
Try with:
SELECT
`id`,
JSON_EXTRACT(json, '$.pixels') as pixels
FROM
`PROD_APPNEXUS`.`dimension_json_creatives`
WHERE
JSON_CONTAINS(`json`, '{"id": 5384}', '$.pixels') AND
`MEMBER_ID` = 364;
Please note that you need a version >= 5.7.8 for MySQL for this to work