Felipe Carreón
Felipe Carreón

Reputation: 53

Add json data to dictionary Python as string

Im trying to add some json data to a dictionary, I already done but not in the way that I want.

I making some requests with Python and in the data that will send I want add some parameters

Here my complete request

def breakoutstats():

cookies = a
headers = {
    'Connection': 'keep-alive',
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'www.site.com',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'www.site.com',
    'Accept-Language': 'en-US,en;q=0.9,es;q=0.8',
}

data = {
'ajax': '1',
'item': 'false',
'timeStamp': '1592832890669600142',
'task': 'getReport',
'divId': 'MainReport',
'reportType': '0',

}
response = requests.post('www.site.com', headers=headers, cookies=cookies, data=data)

The parameter that I want edit is the data , I want to append a json information from a json file, and as mentioned before i already done, but I need to append the information inside singles quotes,

Here is my result:

    data = {
    'ajax': '1',
    'item': 'false',
    'timeStamp': '1592832890669600142',
    'task': 'getReport',
    'divId': 'MainReport',
    'reportType': '0',
    'str' : {'RequestedOutputOptions': '15;23;45;24;26;46', 'FromDate': '2020-06-22 00:00:00', 'ToDate': '2020/06/22 23:59:00', 'CustomerTrafficIds': 'IN6449;6463', 'CustomerIPIds': '', 'ProviderTrafficIds': '', 'ProviderTrunkIds': '', 'ProviderIPIds': '', 'IsMergeCDR': '2', 'INXNames': '', 'InSPXCDRGenIds': '', 'OutSPXCDRGenIds': '', 'RGIds': '', 'GatewayIds': '', 'CountryCodes': 'IN509', 'CityCodes': '509=Mobile Digicel', 'CitiesAndOr': 'AND', 'CityCodes2': '', 'Modalidads': '', 'OperatorIds': '', 'DestinationGroupIds': '', 'CustomCodesGroupIds': '', 'UseTemporaryTable': 'NULL', 'AutoRefreshReport': 0, 'DisconnectCause': '', 'SelectedValues': '', 'ShowInGraph': 0, 'NumOfSample': 'NULL', 'SplitBy': -1, 'FieldToAgreg': 'NULL', 'AnchorFields': -1, 'AutoRefresh': 0, 
'FiltersForRecords': '', 'DailyCycle': 0, 'CompareTableName': '', 'DynamicFilters': '[]'}
    }

And this is what I need.

    data = {
    'ajax': '1',
    'item': 'false',
    'timeStamp': '1592832890669600142',
    'task': 'getReport',
    'divId': 'MainReport',
    'reportType': '0',
    'str': '{"RequestedOutputOptions":"24;22;43;5;27;26;16;2;4;30;44;45;49","FromDate":"2020-06-22 00:00:00","ToDate":"2020/06/22 11:04:00","CustomerTrafficIds":"IN6449;6463","CustomerIPIds":"","ProviderTrafficIds":"","ProviderTrunkIds":"","ProviderIPIds":"","IsMergeCDR":"2","INXNames":"","InSPXCDRGenIds":"","OutSPXCDRGenIds":"","RGIds":"","GatewayIds":"","CountryCodes":"IN509","CityCodes":"509=Mobile Digicel","CitiesAndOr":"AND","CityCodes2":"","Modalidads":"","OperatorIds":"","DestinationGroupIds":"","CustomCodesGroupIds":"","UseTemporaryTable":"NULL","AutoRefreshReport":0,"DisconnectCause":"IN200","SelectedValues":"","ShowInGraph":0,"NumOfSample":"NULL","SplitBy":-1,"FieldToAgreg":"NULL","AnchorFields":-1,"AutoRefresh":0,"FiltersForRecords":"","DailyCycle":0,"CompareTableName":"","DynamicFilters":"[]","Title":"","DownloadToFile":0,"MaxRows":0}'
}

As you can see in my result I got the data without being inside single quotes and inside the brackets also there are single quotes instead double quotes as you can see in the second result.

Here my code that I using to append the info to data dict.

with open('breakoutrequest.json', 'r') as myfile:
    data=myfile.read()
# parse file
obj = json.loads(data)
data.update({'str': obj})

Or are there any other easiest way to edit data in request?

Upvotes: 0

Views: 70

Answers (1)

kamion
kamion

Reputation: 481

Why bother parsing it?

with open('breakoutrequest.json', 'r') as myfile:
    data.update({'str': myfile.read()})

This is what you get:

{
    "ajax": "1",
    "item": "false",
    "timeStamp": "1592832890669600142",
    "task": "getReport",
    "divId": "MainReport",
    "reportType": "0",
    "str": "{\"RequestedOutputOptions\":\"24;22;43;5;27;26;16;2;4;30;44;45;49\",\"FromDate\":\"2020-06-22 00:00:00\",\"ToDate\":\"2020/06/22 11:04:00\",\"CustomerTrafficIds\":\"IN6449;6463\",\"CustomerIPIds\":\"\",\"ProviderTrafficIds\":\"\",\"ProviderTrunkIds\":\"\",\"ProviderIPIds\":\"\",\"IsMergeCDR\":\"2\",\"INXNames\":\"\",\"InSPXCDRGenIds\":\"\",\"OutSPXCDRGenIds\":\"\",\"RGIds\":\"\",\"GatewayIds\":\"\",\"CountryCodes\":\"IN509\",\"CityCodes\":\"509=Mobile Digicel\",\"CitiesAndOr\":\"AND\",\"CityCodes2\":\"\",\"Modalidads\":\"\",\"OperatorIds\":\"\",\"DestinationGroupIds\":\"\",\"CustomCodesGroupIds\":\"\",\"UseTemporaryTable\":\"NULL\",\"AutoRefreshReport\":0,\"DisconnectCause\":\"IN200\",\"SelectedValues\":\"\",\"ShowInGraph\":0,\"NumOfSample\":\"NULL\",\"SplitBy\":-1,\"FieldToAgreg\":\"NULL\",\"AnchorFields\":-1,\"AutoRefresh\":0,\"FiltersForRecords\":\"\",\"DailyCycle\":0,\"CompareTableName\":\"\",\"DynamicFilters\":\"[]\",\"Title\":\"\",\"DownloadToFile\":0,\"MaxRows\":0}"
}

Upvotes: 1

Related Questions