Python Forum
want to know the kind of object whether its a python or json object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
want to know the kind of object whether its a python or json object
#1
import http.client, urllib.request, urllib.parse, urllib.error, base64 headers = {    # Request headers    'Content-Type': 'application/json',    'Ocp-Apim-Subscription-Key': '{subscription key}', } params = urllib.parse.urlencode({    # Request parameters    'numberOfLanguagesToDetect': '{integer}', }) body={  "documents": [    {      "id": "string",      "text": "string"    }  ] } try:    conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')    conn.request("POST", "/text/analytics/v2.0/languages?%s" % params, str(body), headers)    response = conn.getresponse()    data = response.read()    print(data)    conn.close() except Exception as e:    print("[Errno {0}] {1}".format(e.errno, e.strerror)
iam new to python and json.I want to know whether body object used in the code is a json object

or a python object.
Reply
#2
It's neither, rather a dictionary.
It could be saved as a json object, probably would be best choice
Reply
#3
thank you sir.so as i understand "body" is a python dictionary,and in the code the json input is being stored in the python dictionary called "body"..iam i right?..thanks again
Reply
#4
You should use Requests.
Data payload to server is a Python dictionary.
Requests has build in json decoder,it will do encoding inn and out of server(can just call json()).
Eg:
>>> import requests >>> data = {"device_name": "Super-pc", "Type": "DevicePort", "page_size": 2} >>> r = requests.post('http://httpbin.org/post', json=data) >>> r.status_code 200 >>> r.json() {'args': {},  'data': '{"device_name": "Super-pc", "page_size": 2, "Type": "DevicePort"}',  'files': {},  'form': {},  'headers': {'Accept': '*/*',              'Accept-Encoding': 'gzip, deflate',              'Content-Length': '65',              'Content-Type': 'application/json',              'Host': 'httpbin.org',              'User-Agent': 'python-requests/2.9.1'},  'json': {'Type': 'DevicePort', 'device_name': 'Super-pc', 'page_size': 2},  'origin': '83.143.86.75',  'url': 'http://httpbin.org/post'} >>> data = r.json() >>> data['json'].get('device_name', 'Not on server') 'Super-pc' >>> data['json'].get('hello', 'Not on server') 'Not on server'
Reply
#5
You should mask origin key and value from the output :)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Not so important for me,because i am behind VPN(Astrill which i have used for 6-years).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting a DriveItem object from msgraph using relative path mikisDeWitte 1 3,170 Aug-07-2025, 07:50 PM
Last Post: mikisDeWitte
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 886 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  Return a string or byte object from Enum class? Calab 5 1,849 May-14-2025, 05:21 PM
Last Post: snippsat
  How to understand object-oriented programming? snl_9527 3 1,738 May-02-2025, 03:21 PM
Last Post: noisefloor
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 4,815 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  type object 'man' has no attribute 'centerX' Tempo 7 2,263 Mar-07-2025, 03:47 AM
Last Post: deanhystad
  float object can't be interpreted as an integer t23 2 2,529 Jan-18-2025, 07:36 PM
Last Post: deanhystad
  Storing DictProxy object and modifying its nested value? haihal 3 2,506 Dec-20-2024, 04:58 AM
Last Post: deanhystad
  Extract args=, value that was passed to Multiprocessing.Proc object during runtime? haihal 1 1,089 Dec-08-2024, 07:04 AM
Last Post: Gribouillis
  what is solution to save and reload a object? eue 4 5,453 Nov-23-2024, 02:15 PM
Last Post: DeaD_EyE

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.