Search code examples
phpmysqlalgolia

input JSON data type in MySql column


I'm using Algolia as my site's search engine, Algolia wants '_geoloc' attribute to be a JSON object like this in order to enable searching based on locations :-

{
    "rahn": "e369853df766fa44e1ed0ff613f563bd",
    "_geoloc": { "lat" : 23.788117809722195 , "lng" : 86.41868225381391 }
},

But when I exported my Mysql database in JSON its '_geoloc' attribute is like this :-

{
    "sadad": "32rf4rsdf43444rtte4e3rrt3342334",
    "_geoloc": "{ \"lat\" : 23.788117809722195 , \"lng\" : 86.41868225381391 }"
},

Row Info

name :- _geoloc

type :- longtext

collation :- utf8mb4_bin

attributes :- none


Solution

  • Well Since I didn't find any solution to my problem I have done this other alternative i.e. I used Python for doing these changes to my JSON and You can do it too to do any changes to any type of file. Here's my python code to do this:-

    import json
    with open('Your_file_location.json', encoding='cp850') as json_file:
        prd_json_file = json.load(json_file)
        for prd in prd_json_file:
            if(prd['_geoloc'] != ''):
                prd['_geoloc'] = json.loads(prd['_geoloc'])
            else:
                prd['_geoloc'] = { "lat": 23.378536756304715, "lng": 85.33722513906919 }
    with open('Your_file_location.json','w', encoding='cp850') as json_file:
        json.dump(prd_json_file,json_file)
    print('success')