mysql-json

Find in json by key in mysql

一世执手 提交于 2021-01-21 11:20:46
问题 I have a json and i want to extract the value of a key. I know the key name but not the location. My json is complex and would look like this in mysql: set @jsonstr:=' { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook

Remove array element by value in mysql json

旧时模样 提交于 2020-12-06 06:45:17
问题 Is it possible to remove an element by its value (and not its index) in a json array in mysql? For example: # ['new', 'orange'] update waitinglist SET new = JSON_REMOVE("orange", '$') WHERE id=2; # now it will be ['new'] If not, is there a better way to store this, so I can remove elements as needed? Also, there would never be duplicates in the array. 回答1: If you know there are never duplicates in the array, you can use JSON_SEARCH to find the path to the value you want to delete, and then

MYSQL JSON column change array order after saving

大憨熊 提交于 2020-05-26 04:19:50
问题 I am using JSON column type in MySQL database table. When I try to save JSON values in table column, the JSON array automatically re-order(shuffle) I have following JSON: {"TIMER_HEADER": [{"XX!TIMERHDR": "XXTIMERHDR", "VER": " 7", "REL": " 0", "COMPANYNAME": "XXX", "IMPORTEDBEFORE": "N", "FROMTIMER": "N", "COMPANYCREATETIME": "12423426"}, {"XX!HDR": "XXHDR", "PROD": "Qics for Wnows", "VER": "Version 6.0", "REL": "Release R", "IIFVER": "1", "DATE": "2018-01-20", "TIME": "1516520267", "ACCNTNT

How to convert MySQL JSON array to comma separated string

拟墨画扇 提交于 2020-02-15 23:04:29
问题 I have the following phone numbers in a column: ["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"] How can I get that info like this: +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 回答1: i think this is the most only MySQL clean way, atleast for MySQL versions under 8 Query SET SESSION group_concat_max_len = @@max_allowed_packet; SELECT GROUP_CONCAT( JSON_UNQUOTE( JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']')) ) ) FROM ( SELECT @row :

MySQL search json value by key in array

拜拜、爱过 提交于 2020-01-20 06:53:10
问题 I have an array of JSON objects and want to have a specific node returned. To simplify my problem, lets say the array could look like this: [ {"Race": "Orc", "strength": 14}, {"Race": "Knight", "strength": 7}, ... ] And I want to know the strength of the knight for example. The function JSON_SEARCH, returns the path '$[1].Race' and with the path operator I could get the strength. Is there a way to combine those two, so I could do something like the following? SELECT someFunc(myCol,'$[*].Race'

Sorted json array field

删除回忆录丶 提交于 2020-01-15 11:01:50
问题 Is there a function in mysql to sort a json field? SELECT new FROM mytable ["orange", "apple"] ==> Sorting the json array SELECT SORTED(new) FROM mytable ["apple", "orange"] 来源: https://stackoverflow.com/questions/53677820/sorted-json-array-field

Sorted json array field

只愿长相守 提交于 2020-01-15 11:01:07
问题 Is there a function in mysql to sort a json field? SELECT new FROM mytable ["orange", "apple"] ==> Sorting the json array SELECT SORTED(new) FROM mytable ["apple", "orange"] 来源: https://stackoverflow.com/questions/53677820/sorted-json-array-field

How to store a key and value in a table from a JSON object in MySQL

那年仲夏 提交于 2020-01-15 03:42:06
问题 I'm having a MySQL database tables namely ds_message and ds_params, it table ds_message contains a JSON object in each row. I would like to store the key and value of a JSON object into the table ds_params for all the records by referring the ds_message primary key id Table: ds_message _____________________________________________________________________________________ id key_value _____________________________________________________________________________________ 1 '{"a":"John", "b":"bat"

Pop the last item in a JSON Array in MySQL 5.7

核能气质少年 提交于 2020-01-02 08:11:46
问题 I've got an array of dates in a field called from . It can look something like this. ['2016-05-01', '2016-05-03', '2016-05-04'] I want to SELECT the last item (here 2016-05-04 ). I've tried this: SELECT `from`->"$[JSON_LENGTH(`from`) - 1]" FROM `table` WHERE `id` = 3; but got that error: ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 2. I've tried using a variable like this : SET @count = (SELECT JSON_LENGTH(`from`) - 1 FROM `table` WHERE `id` = 3);

Searching inside JSON array in MySQL

核能气质少年 提交于 2019-12-25 09:10:36
问题 I have a table of Products that also contains JSON datatype with JSON array - json_array() field. For the example "Products" table includes the following: ProductID ProductName ProductDesc CreateDate Price HistoricalPrices (json) I am appending the data using the follow query syntax: UPDATE Products SET HistoricalPrices = json_array_append(HistoricalPrices, '$', json_object('CreateDate', '2016-05-01', 'Price', 23.65) ) WHERE ProductID = 1; Example of structure of the JSON array: > [ > { >