0
$\begingroup$

I have a data frame like this.

df=pd.DataFrame({'sub1':[10,20,30,40],'sub2':[5,10,15,20],'sub3':[1,2,3,4]})

and a dictionary like this.

stud={}
stud['name']="abc"
stud['id'] ="AB10"
stud['address']="some_address"

I convert that data frame to dictionary and combine with the stud dictionary.I wrote the following code.Then this stud dictionary should be made in a json format and sent as a web service.

stud={}
stud['name']="abc"
stud['id'] ="AB10"
stud['address']="some_address"
for i in range(0,len(df)):
   data_sample={}
   data_sample_row=df.iloc[i]
   a=data_sample_row.to_dict()
   stud['data_sample']=a
   str_details=str(stud)

I would like to get the result as shown below.

"{'name':'abc','id':'AB10','address':'some_address','data_sample':{'sub1':10,'sub2':5,'sub3':1}}"
"{'name':'abc','id':'AB10','address':'some_address','data_sample':{'sub1':20,'sub2':10,'sub3':2}}"
"{'name':'abc','id':'AB10','address':'some_address','data_sample':{'sub1':30,'sub2':15,'sub3': 3}}"
"{'name':'abc','id':'AB10','address':'some_address','data_sample':{'sub1': 40,'sub2':20,'sub3': 4}}"

I would like to send this as a parameter in requests.post as following.

response=requests.post(url="some_url",data=str_details)

If I don't convert to string, gives me an error, JSONDecodeError: Expecting value: line 2 column 1 (char 1) If I pass json=str_details, as argument gives me the same error, How to resolve this and get the desired results.

$\endgroup$
1
$\begingroup$

To build your data use sth like:

import copy, json

def make_objects(ref_obj, df):
    objects = []
    for i in range(len(df[df.columns[0]].values)):
        cobj = copy.deepcopy(ref_obj)
        cobj['data_sample'] = {}
        for col in df:
            cobj['data_sample'][col] = int(df[col].values[i]) if df[col].dtype == np.int64 else (float(df[col].values[i]) if df[col].dtype == np.float64 else df[col].values[i])
        objects.append(cobj)
    return objects
    
df = pd.DataFrame({'sub1':[10,20,30,40],'sub2':[5,10,15,20],'sub3':[1,2,3,4]})
objects = make_objects({'name':"abc",'id':"AB10",'address':"some_address"}, df)
# in case you want to json-encode the returned objects
json_output = json.dumps(objects)
            
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ @Malathi, ok the solution is to typecast the int64 to simple int in order to be json-serializable. Fixed, check it $\endgroup$ – Nikos M. Jul 9 at 6:28
  • 1
    $\begingroup$ @Malathi, else you can simply stringify the int64 (if you dont want to typecast to simple int) and parse the string at other side. So you can also do: cobj['data_sample'][col] = str(df[col].values[i]). What suits best your requirements $\endgroup$ – Nikos M. Jul 9 at 6:37
  • 1
    $\begingroup$ @Malathi, you can update your question but better post a new question with the details to the problem you face now. $\endgroup$ – Nikos M. Jul 9 at 12:53
  • 1
    $\begingroup$ @Malathi, updated to check dtype of column. You can adjust as you see fit $\endgroup$ – Nikos M. Jul 22 at 15:35
  • 1
    $\begingroup$ @Malathi JSON checked as valid in jsonlint [{"name": "abc", "id": "AB10", "address": "some_address", "data_sample": {"sub1": 10, "sub2": 5, "sub3": 1}}, {"name": "abc", "id": "AB10", "address": "some_address", "data_sample": {"sub1": 20, "sub2": 10, "sub3": 2}}, {"name": "abc", "id": "AB10", "address": "some_address", "data_sample": {"sub1": 30, "sub2": 15, "sub3": 3}}, {"name": "abc", "id": "AB10", "address": "some_address", "data_sample": {"sub1": 40, "sub2": 20, "sub3": 4}}] $\endgroup$ – Nikos M. Jul 23 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.