get json key of postgres column containing a specific word - json

I'm trying to select a key from my db and set its value in a json column with postgres keys are finishing with "_alert".
So in my bd I have a column named data as a json and i just want the keys finishing with "_alert" like "ram_alert", "temperatures_alert", "disk_alert", "cpu_alert".
So I need to get the key and the value to compare with the data I have in my backend app to validate if I need to update the value or dont.
How to do this?
I get all the keys doing select json_object_keys(data) from devices but how to get the key/value pair.. is there a way to use the "like" expression here?

First off, note that your current query will only work if you have one tuple in your 'devices' table. Try inserting another row and you'll get:
ERROR: cannot call json_object_keys on an array
If you're certain that you're only ever going to have ONE result from this table, then the following query should give you what you want:
SELECT key,value FROM devices,json_each(devices.data) where key ~ '_alert$';
I'd still throw something like "LIMIT 1" onto your query to be safe.

Related

How to set primary key starting value in laravel?

I want the primary key value of a table to start in this form in Laravel: 0001.
Here's what I did:
$table->id()->startingValue(0001);
But when a record is created, instead of the "0001" pattern it returns 1.
Yes, I know 0001 is the same thing as 1.
Any idea how I could make it retain that pattern or flip it around?
If you need to save it like that in de MySQL you'll need to use another type like string.
If it's only after db, laravel has mutators.

How to query an array field (AWS Glue)?

I have a table in AWS Glue, and the crawler has defined one field as array.
The content is in S3 files that have a json format.
The table is TableA, and the field is members.
There are a lot of other fields such as strings, booleans, doubles, and even structs.
I am able to query them all using a simpel query such as:
SELECT
content.my_boolean,
content.my_string,
content.my_struct.value
FROM schema.tableA;
The issue is when I add content.members into the query.
The error I get is: [Amazon](500310) Invalid operation: schema "content" does not exist.
Content exists because i am able to select other fiels from the main key in the json (content).
Probably is something related with how to perform the query agains array field in Spectrum.
Any idea?
You have to rename the table to extract the fields from the external schema:
SELECT
a.content.my_boolean,
a.content.my_string,
a.content.my_struct.value
FROM schema.tableA a;
I had the same issue on my data, I really don't know why it needs this cast but it works. If you need to access elements of an array you have to explod it like:
SELECT member.<your-field>,
FROM schema.tableA a, a.content.members as member;
Reference
You need to create a Glue Classifier.
Select JSON as Classifier type
and for the JSON Path input the following:
$[*]
then run your crawler. It will infer your schema and populate your table with the correct fields instead of just one big array. Not sure if this was what you were looking for but figured I'd drop this here just in case others had the same problem I had.

Can I select all same fields of a json field regardless of their path

I have a lot of json files stored in a json data type column in postgres. Now there are plenty of places where the key "warning" can apply. Unfortunately I can not get a json schema so I can not know in advance where exactly all the warning keys can show up. So I would like to do something like this:
select report #> '{*,warning}' from foo;
Is there some way to us wildcards in paths? Or is the only way to dynamically traverse a json value lets say key by key recursively in pl/sql function? (if even possible to return a set of cursors as one big cursor).
EDIT:
Interestingly the good old xml data type can do exactly what I need. So I am a bit puzzled why we can not do the same operations on json documents like:
select xmlexists('//town[text() = ''Toronto'']' PASSING BY REF '<root><oldtowns><town>Toronto</town><town>Ottawa</town></oldtowns><newtowns><town>Toronto</town><town>Ottawa</town></newtowns></root>');
select * from xmltable('//town' PASSING by ref '<root><oldtowns><town>Toronto</town><town>Ottawa</town></oldtowns><newtowns><town>Toronto</town><town>Ottawa</town></newtowns></root>' columns town varchar path 'text()')

Updating one row in JSON stored in MySQL

I've got a longtext field in my MySQL database that contains JSON strings. I'd like to be able to update only one row in the string rather than have to reinsert the entire thing updated.
How can I do that? I'm using Laravel but could do a raw query if needed.
(This is the first time I'm using JSON, so if I'm not using the right terminolgoy, forgive me.)
Your column needs to be of json type. then you can use the JSON_SET to set a aprticular json key in your payload.
example :
update table SETjson_column= JSON_SET(json_column, '$.\"$key\"' , 'foo') where id = 1;

Oracle Select from JSON column with hyphenated keys

Say I have the following stored in a JSON column in an Oracle database:
{
"key-with-hyphens": "value"
}
Assuming this column is called json_column in a table called my_table, then I'm trying to select the value for the "key-with-hyphens" key like so:
select json_column.key-with-hyphens from my_table;
This gives me the following error:
ORA-00936: missing expression
00936. 00000 - "missing expression"
Is there some kind of special syntax you need to use when selecting the value for a hyphenated key from a JSON column in Oracle?
So it turns out that there are a couple ways you can do this.
Method 1:
Doing it the original way I was trying, there were a couple issues with this select:
select json_column.key-with-hyphens from my_table;
First off, key selection in JSON columns will not work unless you alias the table, as such:
select mt.json_column.key-with-hyphens from my_table mt;
However, this still doesn't fix the issue with the hyphenated key. To allow for hyphenated keys in this syntax, you need to wrap the name of the key in quotes:
select mt.json_column."key-with-hyphens" from my_table mt;
The above query will work as expected.
Method 2:
Another way you can do this, without aliasing the table, is using the json_value function in the select clause. You pass the JSON column as the first parameter, and the full path of the key you would like to select as the second parameter, using $ as the root context of the JSON object stored in your column. The following query should also work as expected:
select json_value(json_column, '$."key-with-hyphens"') from my_table;