Hello and good year 2017 to everyone!
I have issues to send a quiet complex json to a cloudant DB. As far as I understand (and it's not really far because i'm a total newbie), the only way to send a file on the cloudant DB with cloudant for python is to make a variable "data"
data = {
'_ID': 'userInfo',
'Name': 'Manu'
'Age': 23,
'Hobbies': ['cloudant', 'python', 'dockers']
}
and to use the command
my_document = my_database.create_document(data)
which succesfully create a file in the DB.
BUT it looks like everything goes trickier when the data is not inside a dict, because at somepoint the .create_document() method try to extract keys and values and it goes wild when you give him a list link to console error: https://www.jepix.fr/images/cloudanterror.png
but it's not a regular list that I try to give him, it's a magnificent list of dicts :-)
[
{dict1},
{dict2},
{dict3},
{dict4},
{ },
{ },
{ },
{ },
{ }
]
with marvelous dicts inside the dicts :-D I think I need a way to convert the list of dicts into a dict of dicts and I ABSOLUTELY HAVE TO keep this dict structure because it is use by other colleagues' templates, parser etc. or my apprentice's bottom will be kicked ruthlessly!
import cloudant
import json
from cloudant.client import Cloudant
client = Cloudant("USR", "PWD", url)
client.connect()
# Client tasks ##########################################
session = client.session()
print 'Username: {0}'.format(session['userCtx']['name'])
print 'Databases: {0}'.format(client.all_dbs())
my_db = client['scenario-json']
with open('scenarioGenerated.json', 'r') as fp:
json_str = fp.read()
json_dict = json.loads(json_str)
my_doc = my_db.create_document(json_dict)
client.disconnect()
I have tried total newbie's desperate move like casting the list into dict with dict() function but I got an argument error telling me I have not enough argument to call this function (at this point I awfully accepted it and searched for another way out)
I also tried the json library for python with json.load/.loads/.dump/.dumps but it's the same problem as it convert the json to a string (looping back on the "str" object has no "get" attribute error)
I saw maybe a solution on an overflow post: Python dump dict to json file
But I need to do the opposite, like reading the json and fill in a dict? I don't think it's the cleverest moves as the dict-filling process can loose the dict hierarchy...
So imo, if you have a nice way to convert the list of dicts into a dict of dicts it would be so nice!
Sorry for the long post and very noobish question but i'm like a plankton in a sea of whales and it's not a good situation :-p
Thank you very much.