How to extract values from nested dictionary using postgresql command? - python

I am using postgresql to extract some data from my database.One of the query i hit returns a nested dictionary to me for a specific date i have specified.
For eg:
query: select user_position_details from user_detail where last_login >= '2018-03-01';
This query return the follwing result:
"{\"user_position\": {\"LOGIN_s\": \"something\", \"X_SLS_AREA_s\": \"data\"}}"
"{\"user_position\": {\"LOGIN_so\": \"login_data\", \"X_SLS_AREA_s\": \"data\"}}"
I want only the field LOGIN_s out of this nested dictionay.
Is there any way i can do this using a specific query?
I have searched about this but could not find anything that would help.
Any help is appreciated.Thank you.

You are getting a json string output. Use the json module to convert it to JSON and access the required value using key.
Ex:
import json
s = ["{\"user_position\": {\"LOGIN_so\": \"something\", \"X_SLS_AREA_s\": \"data\"}}",
"{\"user_position\": {\"LOGIN_so\": \"login_data\", \"X_SLS_AREA_s\": \"data\"}}"]
for i in s:
v = json.loads(i)
print(v["user_position"].get(u'LOGIN_so'))

Related

dynamic query sorting with Peewee

I'm having trouble assembling dynamic sorting with my Peewee python app.
Catalog is a Model object from playhouse.signals
Database is MariaDB
I can sort by 2 fields like this:
resultset = Catalog.select().where(...).order_by(Catalog.app_team,Catalog.domain)
I can use a dynamic sort field like this:
sortlist = []
opt = 'domain'
sortlist.append(getattr(Catalog,opt))
resultset = Catalog.select().where(...).order_by(sortlist)
But I can't see how to do more than one dynamic field. It fails if I pass it a list of 2 objects.
I tried passing a string like 'Catalog.app_team,Catalog.domain' but a string is ignored.
Any help is much appreciated.

Create SQL database from dict with different features in python

I have the following dict:
base = {}
base['id1'] = {'apple':2, 'banana':4,'coconut':1}
base['id2'] = {'apple':4, 'pear':8}
base['id3'] = {'banana':1, 'tomato':2}
....
base['idN'] = {'pineapple':1}
I want to create a SQL database to store it. I normally use sqlite but here the number of variables (features in the dict) is not the same for all ids and I do not know all of them thus I cannot use the standard procedure.
Does someone know an easy way to do it ?
ids will get duplicated if you use the sql i would suggest use postgres as it has a jsonfield ypu can put your data there corresponding to each key. Assuming you are not constrained to use SQL.

Dynamically Create Query String for Django > MySQL from JSON

I am new to Django and am having some issues writing to a MySQL dB. What I am trying to do is loop through nested JSON data, and dynamically create a string to be used with a save() method.
I've looped through my nested JSON data and successfully created a string that contains the data I want to save in a single row to the MySQL table "mysqltable":
q = "station_id='thisid',stall_id='thisstaull',source='source',target='test'"
I then try to save this to the table in MySQL:
b = mysqltable(q)
b.save()
But I am getting the error:
TypeError: int() argument must be a string or a number, not 'mysqltable'
What I think is happening is that it doesn't like the fact I have created a string to use in b = mysqltable(q). When I just write out the statement like the below it works fine:
q = mysqltable(station_id='thisid',stall_id='thisstaull',source='source',target='test')
q.save()
But I am not sure how to take that string and make it available to use with b.save(). Any help would be greatly appreciated!
Instead string, create a dictionary, and then pass it directly to mysqltable:
mysqltable(**dictWithData)
Of course you can re-parse string onto dictionary, but this is useless work...

Python/Django: Get record from DB based on a dictionary field

I have a database table and one of the fields is a serialized python dictionary (or stringified JSON) in this form:
{"full_name":"first_name=John&last_name=Smith", "id":24354,"username":"hello"}
How can i select a record based on this field and specifically on the dictionary's username, if i'm searching for a specific username? I mean is there any smart/fast way to do it without loops or splitting the process, maybe with just one line of code?
#If the field name is "user_info"
account = theModel.filter(user_info=???)
*I know that this design is not so good but i found it that way...
Thanks!
You could use the regex query syntax for Django.
theModel.objects.get(user_info__regex=r'some regex here')

Python Lists and MongoDB insert

Need help in understanding what is happening here and a suggestion to avoid this!
Here is my snippet:
result = [list of dictionary objects(dictionary objects have 2 keys and 2 String values)]
copyResults = list(results);
## Here I try to insert each Dict into MongoDB (Using PyMongo)
for item in copyResults:
dbcollection.save(item) # This is all saving fine in MongoDB.
But when I loop thru that original result list again it shows dictionary objects with a new field added
automatically which is ObjectId from MongoDB!
Later in code I need to transform that original result list to json but this ObjectId is causing issues.No clue why this is getting added to original list.
I have already tried copy or creating new list etc. It still adds up ObjectId in the original list after saving.
Please suggest!
every document saved in mongodb requires '_id' field - which has to be unique among documents in the collection. if you don't provide one, mongodb will automatically create one with ObjectId (bson.objectid.ObjectId for pymongo)
If you need to export documents to json, you have to pop '_id' field before jsonifying it.
Or you could use:
rows['_id'] = str(rows['_id'])
Remember to set it back if you then need to update

Categories

Resources