python 3.6.3 Template.to_json() TypeError: Object of type 'dict_values' is not JSON serializable

949 views
Skip to first unread message

Mario Verbelen

unread,
Dec 7, 2017, 4:40:59 AM12/7/17
to cloudtools-dev
I'm migrating our scripts from python 2.7.14 to 3.6.3 
I stuck on the Template.to_json()

I tried to create a list of dict_values with json.dumps but then I also needed to decode troposphere Importvalue

 File "/home/X/x/git/x/x/eb.py", line 313, in get_json
    return self.template.to_json()
  File "/home/X/.local/lib/python3.6/site-packages/troposphere/__init__.py", line 624, in to_json
    sort_keys=sort_keys, separators=separators)
  File "/home/X/python3/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 430, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 437, in _iterencode
    o = _default(o)
  File "/home/X/python3/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'dict_values' is not JSON serializable

'DEBUG' o value:
o=dict_values([<troposphere.ImportValue object at 0x7fc7e9f83da0>, <troposphere.ImportValue object at 0x7fc7e9f83e80>, <troposphere.ImportValue object at 0x7fc7e9f83c88>])

Mario Verbelen

unread,
Dec 8, 2017, 3:32:18 AM12/8/17
to cloudtools-dev
It seems like the output of values has changed in python3
(where dict.values returns a view of the dictionary’s values instead)

So I needed to change
'Subnets': Join(',', self.private_subnet_ids.values())
to
'Subnets': Join(',', list(self.private_subnet_ids.values()))
Reply all
Reply to author
Forward
0 new messages