+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

Convert python to Json

How do I convert the following python code into json body to be sent with the HTTP request? 

TIA

 
params1 = {
        'e': '{"event_type":"Launched Application","filters":[{"subprop_type":"user","subprop_key":"gp:app_name","subprop_op":"is","subprop_value":["desktop"]}, \
             {"subprop_type":"user","subprop_key":"gp:sku","subprop_op":"contains","subprop_value":["SPEC"]}]}',
        'm': 'uniques',
        'start': '20190101',
        'end': '20200131',
        'i':'30'
    }

 
sachinarorasfsachinarorasf
Hi Yogesh,

Convert python to Json:

Python to JSON (Encoding)

JSON Library of Python performs following translation of Python objects into JSON objects by default
Converting Python data to JSON is called an Encoding operation. Encoding is done with the help of JSON library method – dumps()

dumps() method converts dictionary object of python into JSON string data format.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Here is your JSON response
{
    "e": "{\"event_type\":\"Launched Application\",\"filters\":[{\"subprop_type\":\"user\",\"subprop_key\":\"gp:app_name\",\"subprop_op\":\"is\",\"subprop_value\":[\"desktop\"]},              {\"subprop_type\":\"user\",\"subprop_key\":\"gp:sku\",\"subprop_op\":\"contains\",\"subprop_value\":[\"SPEC\"]}]}",
    "end": "20200131",
    "i": "30",
    "m": "uniques",
    "start": "20190101"
}

To know more please go through the below link:
https://www.guru99.com/python-json.html

Thanks and Regards,
Sachin Arora
www.sachinsf.com
Yogesh BiyaniYogesh Biyani
Hello Sachin,

Thanks for the reply. I tried to pass the JSON to httprequest but I get 405: Method not allowed message.   Any suggestions.

Yogesh
Yogesh BiyaniYogesh Biyani
Here is the python script 
url = 'https://amplitude.com/api/2/events/segmentation'

# json parameters for querying monthly uniques for Launched App between dates
# Add gp: in front of custom user_property
params1 = {
    'e': '{"event_type":"Launched Application","filters":[{"subprop_type":"user","subprop_key":"gp:app_name","subprop_op":"is","subprop_value":["su-desktop"]}, \
         {"subprop_type":"user","subprop_key":"gp:sku","subprop_op":"contains","subprop_value":["PRO"]}]}',
    'm': 'uniques',
    'start': '20190101',
    'end': '20200131',
    'i':'30'
}

# Set to -300000, -3600000, 1, 7, or 30 for real-time, hourly, daily, weekly, and monthly counts,

r = requests.get(url, params = params1,  auth=(api_key, secret_key))
response = r.json()
print( response)

Corresponding apex code 
 
HttpRequest req4= new HttpRequest();
req4.setEndpoint('https://amplitude.com/api/2/events/segmentation');
req4.setMethod('GET');
Blob myBlob1 = Blob.valueof(api_key + ':'+ secret_key);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(myBlob1);
req4.setHeader('Authorization', authorizationHeader);

String params1='{"e": "{\"event_type\":\"Launched Application\",\"filters\":[{\"subprop_type\":\"user\",\"subprop_key\":\"gp:app_name\",\"subprop_op\":\"is\",\"subprop_value\":[\"su-desktop\"]},{\"subprop_type\":\"user\",\"subprop_key\":\"gp:sku\",\"subprop_op\":\"contains\",\"subprop_value\":[\"PRO\"]}]}","end": "20200131","i": "30","m": "uniques","start": "20190101"}';

req4.setBody(params1);

Http http4 = new Http();
HTTPResponse res4 = http4.send(req4);
System.debug(res4.getBody());

I get HTTP 405: Method Not Allowed 
 
sachinarorasfsachinarorasf
Hi Yogesh,

HTTP 405: Method Not Allowed:

The 405 Method Not Allowed is an HTTP response status code indicating that the specified request HTTP method was received and recognized by the server,  but the server has rejected that particular method for the requested resource. 
A 405 code response confirms that the requested resource is valid and exists, but the client has used an unacceptable HTTP method during the request.

To fix the problem go through the below link.

https://airbrake.io/blog/http-errors/405-method-not-allowed

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com