Python Forum
Object of type set is not JSON serializable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object of type set is not JSON serializable
#1
Hi

I've a new problem with json / python.

My json is:
Output:
{ "domaine": { "name": { "service": ["service1", "service2"], "threshold": ["100", "200"] }, ...
On my python script, I'm trying to make a request post to my icinga configuration for monitoring.

#!/usr/bin/env python3 .. with open('/tmp/config.json') as json_file1: data1 = json.load(json_file1) for domaine, value in data1.items(): for name, value2 in value.items(): service = data1[domaine][name]['service'] threshold = data1[domaine][name]['threshold'] try: url = "https://localhost:5665/v1/objects/hosts/" request_url = url+hostname headers = { 'Accept': 'application/json', 'X-HTTP-Method-Override': 'PUT' } data = { "templates": service, "attrs": { "address":"BLABLA", "zone":"BLIBLI", "vars": { threshold } } } r = requests.post(request_url, headers=headers, verify=False, auth=('admin', 'XXXXXXXXXXXXXXXXXX'), data=json.dumps(data))
Result is:
Output:
"vars": { threshold } TypeError: unhashable type: 'list'
If I change
Output:
threshold = data1[domaine][name]['threshold'] to: threshold = tuple(data1[domaine][name]['threshold'])
My result is:
Output:
TypeError: Object of type set is not JSON serializable
I'm trying to change key, format, ... but i'm blocked with this point...

Thanks for help
Reply
#2
{ threshold } is a set with one element. if you pass list to set - it doesn't work because list is mutable, thus not hashable and cannot be element in a set. If you change it to tuple, the set works, but it's not seriazable, as the second error says (i.e. there is no respective type in json).
why you would like a set? what is required as per specifications? I think you misread your specs.

As a side note - fix your indentation and also, don't use spaces after/before braces/brackets i.e. {threshold}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I'm not sure about me for this point ...

In fact if I do my request with curl it works:
Output:
curl -vv -k -X PUT -u "admin:${PASSWORD_API_ADMIN}" -H 'Accept: application/json' -H 'Content-Type: application/json' -L "https://localhost:5665/v1/objects/hosts/hostname", -d "{\"templates\": [\"service1\",\"service2\"], \"attrs\": { \"address\": \"BLABLA\", \"zone\": \"BLIBLI\", \"vars\": {\"threshold1\":100, \"threshlod2\":200} } }"| python3.7 -m json.tool
So perhaps my json format is not good?
I'm a little lost with this json format

Output:
{ "domaine": { "name": { "service": ["service1", "service2"], "threshold": ["threshold1: 100", "threshold2: 200"] }, ...

Seems good.

I've changed my json config with:
Output:
"threshold": {"threshold1": "100", "threshold2": "200"}
and my python code sith:
Output:
"vars": threshold
and it works now
Reply
#4
in curls you have a dict, not set
so
#!/usr/bin/env python3 .. with open('/tmp/config.json') as json_file1: data1 = json.load(json_file1) for domaine, value in data1.items(): for name, value2 in value.items(): service = data1[domaine][name]['service'] threshold1, threshold2 = data1[domaine][name]['threshold'] # unpack the list try: url = "https://localhost:5665/v1/objects/hosts/" request_url = url+hostname headers = { 'Accept': 'application/json', 'X-HTTP-Method-Override': 'PUT' } data = { "templates": service, "attrs": { "address":"BLABLA", "zone":"BLIBLI", "vars": {'threshold1':threshold1, 'threshold2':threshold2} } } r = requests.post(request_url, headers=headers, verify=False, auth=('admin', 'XXXXXXXXXXXXXXXXXX'), data=json.dumps(data))
note that in curl threshold1 and threshold2 are integers, you will read them as str, so you may need to explicitly convert them
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
yeah, that's also possibility, if you can change the input json
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Tanks for help! Have a good day!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  type object 'man' has no attribute 'centerX' Tempo 7 2,263 Mar-07-2025, 03:47 AM
Last Post: deanhystad
  Trying to get JSON object in python and process it further Creepy 2 1,545 Oct-24-2024, 08:46 AM
Last Post: buran
  declaring object parameters with type JonWayn 2 2,089 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  Deserialize Complex Json to object using Marshmallow tlopezdh 2 3,562 Dec-09-2021, 06:44 PM
Last Post: tlopezdh
Star Type Error: 'in' object is not callable nman52 3 5,280 May-01-2021, 11:03 PM
Last Post: nman52
  finding and deleting json object GrahamL 1 6,556 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  Serializable JarredAwesome 4 3,992 Nov-19-2020, 11:50 PM
Last Post: JarredAwesome
  TypeError: 'type' object is not subscriptable Stef 1 8,133 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  isinstance() always return true for object type check Yoki91 2 5,432 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 3,055 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn

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.