I want to use where condition on a json object in a table, in postgreSql. how i need to do this for example: i have a table 'test' it has three columns name(varchar),url(varchar),more(json). i need to retrive date where css21Colors = Purple.
more is a json type and below is the values of more field.
Please let me know what should be syntax of querying for the same?
more = {"colorTree":{"Purple":[{"Spanish Violet":"#522173"}],
"Brown":[{"Dark Puce":"#4e3347"}],"White":[{"White":"#ffffff"}],
"Black":[{"Eerie Black":"#1d0d27"}],"Gray":[{"Rose Quartz":"#a091a4"}]},
"sizeoutscount":0,"css21Colors":{"Purple":69,"Brown":5,"White":4,"Black":17,"Gray":3},
"sizeins": [],"sizeinscount":0,"sizeouts":[],"allsizes":["8","10","16"],
"css3Colors": {"Rose Quartz":3,"White":4,"Dark Puce":5,"Eerie Black":17,"Spanish
Violet":69},"hexColors":{"#522173":69,"#4e3347":5,"#ffffff":4,"#1d0d27":17,"#a091a4":3}}
SELECT more->'css21Colors'->'Purple' FROM test;
Additionally you can query only the rows containing that key.
SELECT
more->'css21Colors'->'Purple'
FROM
test
WHERE
(more->'css21Colors')::jsonb ? 'Purple';
Mind switching to the jsonb data type.
Related
I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()
I have the below string in a column in hive table which i am trying to query using apache drill:
{"cdrreasun":"52","cdxscarc":"20150407161405","cdrend":"20150407155201","cdrdnrar.1un":"24321.70","servlnqlp":"54.201.25.50","men":"42403","xa:lnqruup":"3","cemcau":"120","accuuncl":"21","cdrc:
5","volcuca":"1.7"}
Want to retrieve all values for key cdrreasun using apache drill SQL.
Can't use FLATTEN on the column as it says Flatten does not work with inputs of non-list types.
Can't use KVGEN as well as it works only with MAP datatype.
Drill has function convert_fromJSON which allows converting from String to JSON object. For more details about this function and examples of usage please see https://drill.apache.org/docs/data-type-conversion/#convert_to-and-convert_from
For the example you specified, you can run
convert_fromJSON(colWithJsonText)['cdrreasun']
I figured it out, hope it will be helpful for others.
We have to do it in 3 steps if the datatype is of type MAP:
KVGEN() -> FLATTEN() -> convert_from()
If it's of type STRING then KVGEN() function is not needed.
SELECT ratinggrouplist
,t3.cdrlist3.cdrreason AS cdrreason
,t3.cdrlist3.cdrstart AS cdrstart
,t3.cdrlist3.cdrend AS cdrend
,t3.cdrlist3.cdrduration AS cdrduration
FROM (
SELECT ratinggrouplist, convert_from(t2.cdrlist2.`element`, 'JSON') AS cdrlist3
FROM (
SELECT ratinggrouplist ,flatten(t1.cdrlist1.`value`) AS cdrlist2
FROM (
SELECT ratinggrouplist, kvgen(cdrlist) AS cdrlist1
FROM dfs.tmp.SOME_TABLE
) AS t1
) AS t2
) AS t3;
I'm new to MySQL. I'm trying to add a string value to a json value in MySQL. The column name is IPConfig. This is the current json string in the column.
{"theme":"black", "button1link":"http//sample.com", "name":"pp"}
I have to append a "www" to button1link value.
Thanks in advance!
Here you can try
UPDATE table SET DATA= JSON_SET(DATA, "$.button1link", INSERT("http//sample.com", 7, 0,'www')) WHERE 1 = 1;
But for this to work, you will need MySQL 5.7+
You can have insert function docs here.
The table definition is:
chat_id serial primary key, last_update timestamp, messages JSON[]
and I want to insert a record like this:
insert into chats (messages) values ('{{"sender":"pablo","body":"they are on to us"}}');
with error:
ERROR: malformed array literal: "{{"sender":"pablo","body":"they are on to us"}}"
LINE 1: insert into chats (messages) values ('{{"sender":"pablo","bo...
I have also tried this approach :
insert into chats (messages) values (ARRAY('{"sender":"pablo","body":"they are on to us"}'));
Note that updating the row and inserting with array_append works OK.
I think this is a clash between the JSON notation that starts with { and the short hand array notation in Postgres where the string representation of an array is also denoted by an {.
The following works:
insert into chats
(messages)
values
(array['{"sender":"pablo","body":"they are on to us"}']::json[]);
This avoids the ambiguity of the {{ by using an explicit array constructor.
To make the array a json array you need to either cast the string to a json or the resulting array to a json[] (see the example above). Casting the whole array makes it easier if you have more than one JSON document in that row:
insert into chats
(messages)
values
(array['{"sender":"pablo","body":"they are on to us"}',
'{"sender":"arthur"}']::json[]);
alternatively:
insert into chats
(messages)
values
(array['{"sender":"pablo","body":"they are on to us"}'::json,
'{"sender":"arthur"}'::json]);
It is quite complicate to escape special characters. I would use insert from select to make it easier.
Create JSON array.
Convert it to set of JSON variables.
Convert the set to SQL array.
The select part below.
WITH bu AS (SELECT json_array_elements('[{"name":"obj1"},{"name":"obj2"},{"name":"obj3"},{"name":"obj4"}]'::json) AS bu)
SELECT array_agg(bu) FROM bu;
I have the following linq to sql query:
DateTime linqdate = (from de in dvlist
where de.DataValue == value
select de.DateTime);
I want to get the date value form database in a datetime variable but I got the following error:
cannot implicitly convert type
'System.Collections.Generic.IEnumerable'
to 'System.DateTime'
any ideas where the problem is? thanks in advance
A Linq query returns an IEnumerable<T> that you can iterate or you can convert it to another type of object (using some extension methods)
In order to get what you want you should do something like this :
var dateTime=(from de in dvlist
where de.DataValue == value
select de.DateTime).FirstOrDefault();
this way you are returning the first element of your enumerable object, or the default value for that type (T, in this case DateTime) if there is no match in the query.