Postgresql - text Array to Json Array error - sql

I am getting an error while trying to convert string array to json array in postgresql.
SQL:
select * from
jsonb_array_elements_text(to_jsonb('[{\"Apr2021\":\"1.2\",\"Aug2000\":\"1.3\",\"Dec2023\":\"22.5\",\"Feb2023\":\"66.7\",\"Jan2023\":\"99.1\",\"Jul2023\":\"11.0\",\"Jun2021\":\"44.2\",\"Mar2023\":\"55\",\"May2023\":\"10\",\"Nov2023\":\"44\",\"Oct2023\":\"99\",\"Sep2023\":\"33\"}]'::json))
Error:
> Invalid operation: invalid input syntax for type json Details: Token
> "Apr2021" is invalid.

Related

Kusto malformed string literal

Can anyone suggest what is wrong with below Kusto. I get error as below. I am trying to export data from my External table to a storage account
Error
Method 'ParseStringLiteral' invoked with an invalid argument 'literal', details: 'Malformed string literal: '<missing STRINGLITERALX>''
Query(logsKube is my external table name):
.export async compressed to json
(
h#"https://azdevstoreforlogs.blob.core.windows.net/exportinglogs;JnAU9secretmUGqNa4/4F8WE+MIWj8z9FQ==",
)
<|'logsKube'| limit 10000
New Query as suggested in answer below but get same error:
.export async compressed to json
(
h#"https://azdevstoreforlogs.blob.core.windows.net/exportinglogs;YBLLh3FY20tVI6a2fQ==",
)
<|external_table('logsKube')| limit 10000
Your query is invalid.
1
External table should be referenced as following:
external_table('logsKube ')
2
You have extra comma after the storage URL

How to resolve this sql error of schema_of_json

I need to find out the schema of a given JSON file, I see sql has schema_of_json function
and something like this works flawlessly
> SELECT schema_of_json('[{"col":0}]');
ARRAY<STRUCT<`col`: BIGINT>>
But if I query for my table name, it gives me the following error
>SELECT schema_of_json(Transaction) as json_data from table_name;
Error in SQL statement: AnalysisException: cannot resolve 'schemaofjson(`Transaction`)' due to data type mismatch: The input json should be a string literal and not null; however, got `Transaction`.; line 1 pos 7;
The Transaction is one of the columns in my table and after checking it manually I can attest that it is of String type(json).
The SQL statement has it to give me the schema of the JSON, how to do it?
after looking further into the documentation that it is clear that the word foldable means that of the static one, and a column from a table JSON won't work
for minimal reroducible example here you go:
SELECT schema_of_json(CAST('{ "a": "b" }' AS STRING))
As soon as the cast is introduced in the above statement, the schema_of_json will fail......... It needs a static JSON as it's input

PostgreSQL - Query nested json in text column

My situation is the following
-> The table A has a column named informations whose type is text
-> Inside the informations column is stored a JSON string (but is still a string), like this:
{
"key": "value",
"meta": {
"inner_key": "inner_value"
}
}
I'm trying to query this table by seraching its informations.meta.inner_key column with the given query:
SELECT * FROM A WHERE (informations::json#>>'{meta, inner_key}' = 'inner_value')
But I'm getting the following error:
ERROR: invalid input syntax for type json
DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1:
SQL state: 22P02
I've built the query following the given link: DevHints - PostgreSQL
Does anyone know how to properly build the query ?
EDIT 1:
I solved with this workaround, but I think there are better solutions to the problem
WITH temporary_table as (SELECT A.informations::json#>>'{meta, inner_key}' as inner_key FROM A)
SELECT * FROM temporary_table WHERE inner_key = 'inner_value'

Trying to covert long to ToDate format

My input is long "20190503143744" and wanted to convert to format "2019-09-06 11:46:22"
Trying below code:
A = LOAD 'stp_master_subscriber_profile' using org.apache.hive.hcatalog.pig.HCatLoader() as (mdn:chararray, imei:chararray, imsi:chararray, subscriber_id:long, manufacturer:chararray, model:chararray, update_time:long, scenario:chararray, vz_customer:chararray, commit_time:long);
B = FOREACH A GENERATE ToString(ToDate((chararray)commit_time,'yyyyMMdd'),'yyyy-MM-dd HH:mm:ss') as event_date_gmt:chararray;
Getting error:
ERROR 1066: Unable to open iterator for alias B. Backend error : org.apache.pig.backend.executionengine.ExecException: ERROR 0: Exception while executing [POUserFunc (Name: POUserFunc(org.apache.pig.builtin.ToDate2ARGS)[datetime] - scope-15 Operator Key: scope-15) children: null at []]: java.lang.IllegalArgumentException: Invalid format: "20190503143744" is malformed at "143744"
The issue is that you're specifying the format as yyyyMMdd but your original input is in yyyyMMddHHmmss format, so you get an error when Pig reaches 143744 instead of the end of your string. Try this:
B = FOREACH A GENERATE ToString(ToDate((chararray)commit_time,'yyyyMMddHHmmss'),
'yyyy-MM-dd HH:mm:ss') as event_date_gmt;

Invalid input syntax for type date: "\N" in postgresql when fetching data

I'm using postgresql and getting this sql error:
org.postgresql.util.PSQLException: ERROR:
invalid input syntax for type date: "\N"
Here is my code:
SELECT * FROM person WHERE person.dob = '\N' OR person.dob = '1994-01-16';
How can I allow Null values? so what should I add to this SQL statement?
Try this following code:
SELECT * FROM person WHERE person.dob is null OR person.dob = '1994-01-16';