I'm trying to perform a SELECT at a PostgreSQL table with a JSON column. The problem is that that column is not a real JSON.
When I export to CSV the result is the following:
"{""access"":""1.SQnGhZKY4cemiRU+Svteoj2ZPQJ74uqXoWmGg0BmVf8ho3D2c7g1xYogcwz"",""objectId"":""58e3a109b48a6"",""deviceId"":""58e65028b4567"",""deviceBusinessUnits"":[],""crmId"":""443-f01246e5"",""crmBusinesUnits"":[],""crm"":{""gender"":""m"",""birthDate"":"""",""age"":null,""customFields"":{""displayName"":""te4"",""givenName"":""test"",""familyName"":""test"",""email"":""tes#yopil.net"",""mobileNumber"":""68"",""mobileNumberCountryCode"":""34"",""residencyCountry"":""LUX"",""mailingStreetName1"":null,""mailingStreetName2"":null,""mailingSubAdministrativeArea"":null,""mailingMunicipality"":null,""mailingPostalCode"":""208"",""mailingAdministrativeArea"":null,""mailingCountry"":null,""shippingStreetName1"":null,""shippingStreetName2"":null,""shippingSubAdministrativeArea"":null,""shippingMunicipality"":null,""shippingPostalCode"":null,""shippingAdministrativeArea"":null,""shippingCountry"":null},""tags"":[],""businessUnits"":[],""crmProvider"":{""type"":""cid"",""sessionToken"":""7drmcvu""}}}"
My column is named "user" and my query is:
select
mytable.user ->> 'accessToken' as userRaw
from mytable;
But I get:
How could I get the JSON values from this column?
Related
I'm trying to fetch all rows where a nested JSON value is in an array of values, but I'm getting : Unknown column 'metadata' in 'where clause'
SQL:
SELECT JSON_EXTRACT(eagerly_loaded_data, "$.Metadata") AS metadata
FROM worker_items
WHERE metadata IN ('metadata1', 'metadata2');
Hi I have a mysql table with a JSON column. Lets say table name is example_table and JSON column name is json_column and I have a JSON value x that I want to query by.
I was wondering if there is someway to query on the table to see if a value inside example_table that has a json_column value equal to x.
So something like this:
select * from example_table where json_column=x;
Example where x is a JSON array.
select * from example_table where json_column='["12345", "56789"]';
The above query does not work. I was wondering how I can query the table this way?
I figured it out.
select * from example_table where json_column=CAST('["12345", "56789"]' AS JSON);
select id,name,age from person where id=1;
This query gives result like below
id | name | age
1 |manoj | 20
I want a JSON like below
"{"id":1,"name":"manoj","age":5}"
I want a dynamic way.When I try another query from another table ,that result as like previous JSON
I want to generate JSON from a table and store into a column in MYSQL, I don't want to use php or other server side language for generate this JSON.
How can I get JSON in MYSQL ?
Use the JSON_OBJECT() function:
SELECT JSON_OBJECT('id', id, 'name', name, 'age', age)
FROM person
WHERE id = 1;
This requires at least MySQL 5.7 or MariaDB 10.2.3, that's when all the JSON-related functions were added.
If you don't want to hard-code the column names into the query, you'll need to write a stored procedure that creates dynamic SQL, using INFORMATION_SCHEMA.COLUMNS to get all the column names.
Look into JSON data type and JSON functions. If column is of JSON data type and insert string is in JSON format it's easy to insert and retrieve too.
MySQL JSON data type
JSON functions
I am trying to load de-serialized json events into different tables, based on the name of the event.
Right now I have all the events in the same table, the table has only two columns EventName and Payload (the payload stores the json representation of the event):
CREATE TABLE event( EventName STRING, Payload STRING)
So basically what i want is to load the data in the following table:
CREATE TABLE TempEvent ( Column1 STRING, Column2 STRING, Column3 STRING )
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
STORED AS TEXTFILE;
And load the events with something like:
INSERT INTO TempEvent select Payload from event where EventName='TempEvent';
But hive is throwing an exception saying that the destination table has 3 columns, and the select statement just 1.
Is there other way to acomplish this or i am doing something wrong?.
The JSON serde requires a table with one JSON per line in order to use it. So it won't work with your input table because the line
TempEvent, {"Column1":"value1","Column2":"value2","Column3":"value3"}
is not a valid JSON. So first you need to move it into a new intermediate table which just contains valid JSON, and then populate the JSON serde table from there using load data:
create table event_json (Payload string)
stored as textfile;
insert into table event_json
select Payload from event
where EventName='TempEvent';
create table TempEvent (Column1 string, Column2 string, Column3 string)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';
load data inpath '/user/hive/warehouse/event_json' overwrite into table TempEvent;
Then you can extract the columns like this:
select Column1, Column2, Column3
from TempEvent;
Of course all of this processing would not be necessary if your source table was valid JSON originally, you could just create the TempEvent table as as an external table and pull data directly from it.
I have a json stored as text in one of my database row. the json data is as following
[{"id":67272,"name":"EE_Quick_Changes_J_UTP.xlsx"},{"id":67273,"name":"16167.txt"},{"id":67274,"name":"EE_12_09_2013_Bcum_Searchall.png"}]
to parse this i want to use postgresql method
json_populate_recordset()
when I post a command like
select json_populate_recordset(null::json,'[{"id":67272,"name":"EE_Quick_Changes_J_UTP.xlsx"},{"id":67273,"name":"16167.txt"},{"id":67274,"name":"EE_12_09_2013_Bcum_Searchall.png"}]') from anoop;
it gives me following error
first argument of json_populate_recordset must be a row type
note : in the from clause "anoop" is the table name.
can anyone suggest me how to use the json_populate_recordset method to extract data from this json string.
I got method's reference from
http://www.postgresql.org/docs/9.3/static/functions-json.html
The first argument passed to pgsql function json_populate_recordsetshould be a row type. If you want to use the json array to populate the existing table anoop you can simply pass the table anoop as the row type like this:
insert into anoop
select * from json_populate_recordset(null::anoop,
'[{"id":67272,"name":"EE_Quick_Changes_J_UTP.xlsx"},
{"id":67273,"name":"16167.txt"},
{"id":67274,"name":"EE_12_09_2013_Bcum_Searchall.png"}]');
Here the null is the default value to insert into table columns not set in the json passed.
If you don't have an existing table, you need to create a row type to hold your json data (ie. column
names and their types) and pass it as the first parameter, like this anoop_type:
create TYPE anoop_type AS (id int, name varchar(100));
select * from json_populate_recordset(null :: anoop_type,
'[...]') --same as above
no need to create a new type for that.
select * from json_populate_recordset(null::record,'[{"id_item":1,"id_menu":"34"},{"id_item":2,"id_menu":"35"}]')
AS
(
id_item int
, id_menu int
)