MySQL split first and second element from one column to different colmns
I have three columns in my DataBase
ID | boundary_coord | lat | lng |
------------------------------------------------------------
1 | [[27.913308738086336,78.0780080756358], | | |
| [21.913355835273993,75.07837828047921], | | |
| [26.91360564733089,32.07834574752177], | | |
| [23.9136475694797,35.07799837107608], | | |
| [34.913308738086336,34.0780080756358]] | | |
2 | [[16.966884703254756,79.30961584505829],| | |
| [17.967035001856917,73.30904577055392], | | |
| [13.967680163832473,74.30925753680006], | | |
| [15.967591705852122,71.30952320375857], | | |
| [12.96751655678886,77.30982165205582], | | |
| [14.966884703254756,72.30961584505829]] | | |
Now I am trying the get first value from the boundary_coord put it in the lat column and get the second value from boundary_coord, put it the lng column
So in Mysql I wrote this
SELECT SUBSTRING_INDEX(boundary_coord,',',2) AS LAT_LNG, farm_name FROM `farm_management_farm` WHERE boundary_coord is NOT Null
OUTPUT
LAT_LNG
-------------------------------------------
[[27.913308738086336,78.0780080756358]
[[16.966884703254756,79.30961584505829]
Clearly, this is not the output I am expecting and one more prob that is in the starting I am getting two openings [[
Expected output
ID | boundary_coord | lat | lng |
-----------------------------------------------------------------------------------
1 | [[27.913308738086336,78.0780080756358], |27.913308738086336|78.0780080756358 |
| [21.913355835273993,75.07837828047921], | | |
| [26.91360564733089,32.07834574752177], | | |
| [23.9136475694797,35.07799837107608], | | |
| [23.9136475694797,35.07799837107608]] | | |
2 | [[16.966884703254756,79.30961584505829],|16.966884703254756|79.30961584505829|
| [17.967035001856917,73.30904577055392], | | |
| [13.967680163832473,74.30925753680006], | | |
| [15.967591705852122,71.30952320375857], | | |
| [14.966884703254756,72.30961584505829], | | |
| [14.966884703254756,72.30961584505829]] | | |
You can use JSON_EXTRACT
to parse the array you have in boundary_coord like so:
SELECT id, boundary_coord,
JSON_EXTRACT(boundary_coord, '$[0][0]') AS lat,
JSON_EXTRACT(boundary_coord, '$[0][1]') AS lng
FROM `farm_management_farm` WHERE boundary_coord IS NOT NULL;