In my hive table "ticket_full" I have a json type column named "service_id" that I would like to extract in 3 columns, which is like this
[{"position":"1","typeid":"ROUTNAME","value":"PWAW13197"},{"position":"2","typeid":"CDCNAME","value":null},{"position":"3","typeid":"SVCNAME","value":"Business"},{"position":"4","typeid":"USID","value":"FI021MLQE4"}]
[{"position":"1","typeid":"ROUTNAME","value":"KHLA30076"},{"position":"2","typeid":"CDCNAME","value":"eff-e-rjh-sw-cs2"},{"position":"3","typeid":"SVCNAME","value":"Managed LAN"},{"position":"4","typeid":"USID","value":"SA00BNGH0E"}]
[{"position":"1","typeid":"NUMLIAPTT","value":"0492212984"},{"position":"2","typeid":null,"value":null},{"position":"3","typeid":null,"value":null},{"position":"4","typeid":null,"value":null}]
I used the code below:
SELECT get_json_object(single_json_table.identifiant_produit, '$.position') AS position,
get_json_object(single_json_table.identifiant_produit, '$.typeid') AS typeid,
get_json_object(single_json_table.identifiant_produit, '$.value') AS value
FROM
(SELECT explode(split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
'"},\\{"', '"},,,,{"'), ',,,,') ) as identifiant_produit
FROM ticket_full) single_json_table
it works but every time there is a value at NULL, it ignores what follows and goes to the next field:
example:
Does anyone know how to fix this please ?
It is because null has no double-quotes and you are replacing this '"},\\{"' with this '"},,,,{"'
Try to remove double-quote before } in the regex pattern and replacement string accordingly, then it will work with quoted values and nulls also:
split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
'},\\{"', '},,,,{"'), ',,,,')
Related
I have some JSON data in Redshift table of type character varying. An example entry is:
[{"value":["*"], "key":"testData"}, {"value":"["GGG"], key: "differentData"}]
I want to return vales based on keys, how can i do this? I'm attempting to do something like
json_extract_path_text(column, 'value') but unfortunately it errors out. Any ideas?
So the first issue is that your string isn't valid JSON. There are mismatched and missing quotes. I think you mean:
[{"value":["*"], "key":"testData"}, {"value":["GGG"], "key": "differentData"}]
I don't know if this is a data issue or a transcription error but these functions won't work unless the json text is valid.
The next thing to consider is that at the top level this json is an array so you will need to use json_extract_array_element_text() function to pick up an element of the array. For example:
json_extract_array_element_text('json string', 0)
So putting this together we can extract the first "value" with (untested):
json_extract_path_text(
json_extract_array_element_text(
'[{"value":["*"], "key":"testData"}, {"value":["GGG"], "key": "differentData"}]', 0
), 'value'
)
Should return the string ["*"].
I have an outlet_details table having two columns(id and extended_attributes as a JSON object).
extended_attributes have values like
{
"parent-0-0-id": "DS-606",
"parent-0-1-id": "SD066",
"secondaryOutletCode": "MG_918"
}
I want to get parent-0-0-id's value, but when I'm trying to hit
SELECT extended_attributes->>'$.parent-0-0-id' AS 'parent00id' FROM outlet_details;
I'm getting an:
invalid JSON path expression error(3143).
You could just enclose the column name under quotes to separate out the name from escape characters.
SELECT extended_attributes->>"$.\"parent-0-0-id\"" AS 'parent00id' FROM outlet_details; should work
Here is the sample row value
CREATION_DATE: 2021-08-30
USER_EMAIL: 'jack#sample.com
ACTION: 'FileAccessedExtended'
AUDITDATA:
{
"OrganizationId":"orgid1231231",
"RecordType":6,
"UserKey":"i:0h.f|membership|10032000de9e9feb#live.com",
"UserType":0,
"Version":1,
"Workload":"SharePoint",
"ClientIP":"11.11.11.11",
"ObjectId":"https:\/\/someorganization.sharepoint.com\/sites\/MondayMeetings862\/Shared Documents\/folder\/folders\/somefolder\/Contact Database\/Investment Contact Database (MASTER) 1.xlsx",
"UserId":"jack#sample.com",
"CorrelationId":"12-031-23809123809172309",
"EventSource":"SharePoint",
"ItemType":"File",
"ListId":"334b5378-2a4c-4ba6-b1ac-1ccb51a38687",
"ListItemUniqueId":"a46a8f64-0ecf-415b-b372-32852c160d74",
"Site":"2839182391823819238192389",
"UserAgent":"MSOCS",
"WebId":"1092301723729813",
"HighPriorityMediaProcessing":false,
"SourceFileExtension":"xlsx",
"SiteUrl":"https:\/\/someorganization.sharepoint.com\/sites\/MondayMeetings862\/",
"SourceFileName":"Investment Contact Database (MASTER) 1.xlsx",
"SourceRelativeUrl":"Shared Documents\/folder\/folders\/COMMERCIAL\/Contact Database"
}
Desired output:
CREATION_DATE: 2021-08-30
USER_EMAIL: Jack#sample.com
ACTION: File Access Extended
CLIENTIP: 11.11.11.11
Assuming auditdata is a column of type jsonb (or at least json) you can use the ->> operator to extract a value from it:
select creation_date, user_email, action,
auditdata ->> 'ClientIP' as client_ip
from the_table;
If for some reason, the column is defined properly as a jsonb (or at least json) you need to cast the value auditdata::jsonb ->> 'ClientIP'
Ok. Here come a bunch of if's. If AuditData is an array. And if ClientIP is reliably in the same position, then
SELECT AuditData[6] FROM MyTable
should give
"ClientIP":"11.11.11.11"
You can remove the double quotes by using the string function replace to replace double quote with nothing.
I have a JSON column in an Oracle DB where it was populated without the ABSENT ON NULL option and there are some pretty long lengths because of this.
I would like to trim things down and have created a new table similar to the first but I would like to select the JSON from form the old, add the ABSENT ON NULL option and place the new values in reducing the column length.
So I can see the JSON easy enough like
SELECT json_query(json_data,'$') FROM table;
This will give a result like:
{
"REC_TYPE_IND":"1",
"ID":"1234",
"OTHER_ID":"4321",
"LOCATION":null,
"EFF_BEG_DT":"19970101",
"EFF_END_DT":"99991231",
"NAME":"Joe",
"CITY":null
}
When I try to remove the null values like
SELECT json_object (json_query(json_data,'$') ABSENT ON NULL
RETURNING VARCHAR2(4000)
) AS col1 FROM table;
I get the following:
ORA-02000: missing VALUE keyword
I assume this is because the funcion json_object is expecting the format:
json_object ('REC_TYPE_IND' VALUE '1',
'ID' VALUE '1234')
Is there a way around this, to turn the JSON back into values that JSON_OBJECT can recognize like above or is there a function I am missing?
I've been trying to use dynamic columns with an instance of MariaDB v10.1.12.
First, I send the following query:
INSERT INTO savedDisplays (user, name, body, dataSource, params) VALUES ('Marty', 'Hey', 'Hoy', 'temp', COLUMN_CREATE('type', 'tab', 'col0', 'champions', 'col1', 'averageResults'));
Where params' type was defined as a blob, just like the documentation suggests.
The query is accepted, the table updated. If I COLUMN_CHECK the results, it tells me it's fine.
But when I try to select:
"SELECT COLUMN_JSON(params) AS params FROM savedDisplays;
I get a {type: "Buffer", data: Array} containing binary returned to me, instead of the {"type":"tab", "col0":"champions", "col1":"averageResults"} I expect.
EDIT: I can use COLUMN_GET just fine, but I need every column inside the params field, and I need to check the type property first to know what kind of and how many columns there are in the JSON / params field. I could probably make it work still, but that would require multiple queries, as opposed to only one.
Any ideas?
Try:
SELECT CONVERT(COLUMN_JSON(params) USING utf8) AS params FROM savedDisplays
In MariaDB 10 this works at every table:
SELECT CONVERT(COLUMN_JSON(COLUMN_CREATE('t', text, 'v', value)) USING utf8)
as json FROM test WHERE 1 AND value LIKE '%12345%' LIMIT 10;
output in node.js
[ TextRow { json: '{"t":"test text","v":"0.5339044212345805"}' } ]