1

Error while parsing map to string using json.dumps in python 3.6

x = {'id_str': '639035115457388544', 'video': False, 'photo': False, 'link': True, 'hashtags': <map object at 0x7f1762ab9320>, 'coordinates': None, 'timestamp_ms': 1441218018000, 'text': 'Police suspected hit-and-run', 'user': {'id': 628694263, 'name': 'Beth LeBlanc', 'friends_count': 235, 'verified': False, 'followers_count': 654, 'created_at': 1341631106000, 'time_zone': None, 'statuses_count': 3966, 'protected': 3966}, 'mentions': [], 'screen_name': 'THBethLeBlanc', 'reply': None, 'tweet_type': 'Tweet', 'mentionedurl': None, 'possibly_sensitive': False, 'placename': '', 'sentiments': 'Undefined'}

print(json.dumps(x))
TypeError: Object of type 'map' is not JSON serializable
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
Yash
  • 31
  • 1
  • 6
  • how did you generate the `x`? where did you assign value to `hashtags`? – Kushan Gunasekera Jul 23 '19 at 05:12
  • 1
    using a method like below: def process_tweets(doc): tweet = {'id_str': doc['id_str'], 'video': False, 'photo': False, 'link': False} if doc['entities'].get('media'): tweet['photo'] = True if doc.get('extended_entities'): tweet[doc['extended_entities']['media'][0]['type']] = True tweet['mediaurl'] = doc['extended_entities']['media'][0]['media_url'] if doc['entities'].get('urls'): tweet['link'] = True tweet['hashtags'] = map(lambda x: x['text'], doc['entities']['hashtags']) tweet['coordinates'] = doc['coordinates'] – Yash Jul 23 '19 at 05:15

2 Answers2

3

I don't know how you get value for 'hashtags', but this below example will help you to solve your question a little bit. Surround your map object with list().

>>> import json
>>> 
>>> some_map_value = map([],[])
>>> some_map_value
<map object at 0x7f380a75a850>
>>> 
>>> x = {'hashtags': some_map_value}
>>> x
{'hashtags': <map object at 0x7f380a75a850>}
>>> 
>>> json.dumps(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type map is not JSON serializable
>>> 
>>> list(some_map_value)
[]
>>> x = {'hashtags': list(some_map_value)}  # surround your map object with list
>>> json.dumps(x)
'{"hashtags": []}'

For more information check this Getting a map() to return a list in Python 3.x Ask Question question. If this is not you are lokking for, please put a comment to this answer.

Update: Just check your comment. Surround your map(lambda x: x['text'],doc['entities']['hashtags']) with list() like list(map(lambda x: x['text'],doc['entities']['hashtags']))

if doc['entities'].get('media'):
    tweet['photo'] = True
if doc.get('extended_entities'):
    tweet[doc['extended_entities']['media'][0]['type']] = True
    tweet['mediaurl'] = doc['extended_entities']['media'][0]['media_url']
if doc['entities'].get('urls'):
    tweet['link'] = True
    tweet['hashtags'] = list(map(lambda x: x['text'],doc['entities']['hashtags']))
    tweet['coordinates'] = doc['coordinates']
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • 1
    It worked! I missed the list around the map. Thank you! – Yash Jul 23 '19 at 05:39
  • You're welcome and happy hear about it @Yash. If this helpful for you please select this answer as accepted one by clicking the correct button below the votes of this answer. Thank you too! – Kushan Gunasekera Jul 23 '19 at 05:42
0

There is an error in your x where the hashtags key has no corresponding value. Here it is fixed: https://repl.it/repls/SubtleLovableSystemadministrator