0
[ 
   { 
      'topleft':{ 
         'x':34,
         'y':13
      },
      'confidence':0.17681329,
      'bottomright':{ 
         'x':398,
         'y':347
      },
      'label':'person'
   },
   { 
      'topleft':{ 
         'x':29,
         'y':48
      },
      'confidence':0.107129775,
      'bottomright':{ 
         'x':399,
         'y':351
      },
      'label':'car'
   },
   { 
      'topleft':{ 
         'x':20,
         'y':85
      },
      'confidence':0.22963998,
      'bottomright':{ 
         'x':376,
         'y':350
      },
      'label':'cat'
   },
   { 
      'topleft':{ 
         'x':0,
         'y':2
      },
      'confidence':0.12423642,
      'bottomright':{ 
         'x':372,
         'y':356
      },
      'label':'sheep'
   },
   { 
      'topleft':{ 
         'x':20,
         'y':12
      },
      'confidence':0.26517922,
      'bottomright':{ 
         'x':378,
         'y':349
      },
      'label':'dog'
   }
]

This is an array of object from that was returned from TensorFlow. However, while trying to convert to a JSON object using json.dumps, I got this error TypeError: 0.17681329 is not JSON serializable

I have tried to use simplejson and got Float32 is not serializable. How can I convert this array of objects to be JSON serializable?

ruohola
  • 16,015
  • 6
  • 33
  • 67
Eneres
  • 181
  • 1
  • 10

2 Answers2

2

You have a Decimal or some numpy type in there, that needs to be converted to float.

import json
from decimal import Decimal

import numpy


class CustomJsonEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, Decimal):
            return float(obj)
        elif isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        return super(CustomJsonEncoder, self).default(obj)

Then call dumps as follows:

json.dumps(data, cls=CustomJsonEncoder)
ruohola
  • 16,015
  • 6
  • 33
  • 67
alexisdevarennes
  • 4,602
  • 3
  • 21
  • 37
0

Works for me:

Python 2.7.15+ (default, Oct  7 2019, 17:39:04) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [{'topleft': {'x': 34, 'y': 13}, 'confidence': 0.17681329, 'bottomright': {'x': 398, 'y': 347}, 'label': 'person'}, {'topleft': {'x': 29, 'y': 48}, 'confidence': 0.107129775, 'bottomright': {'x': 399, 'y': 351}, 'label': 'car'}, {'topleft': {'x': 20, 'y': 85}, 'confidence': 0.22963998, 'bottomright': {'x': 376, 'y': 350}, 'label': 'cat'}, {'topleft': {'x': 0, 'y': 2}, 'confidence': 0.12423642, 'bottomright': {'x': 372, 'y': 356}, 'label': 'sheep'}, {'topleft': {'x': 20, 'y': 12}, 'confidence': 0.26517922, 'bottomright': {'x': 378, 'y': 349}, 'label': 'dog'}]
>>> import json
>>> print json.dumps(a, indent=4)
[
    {
        "topleft": {
            "y": 13, 
            "x": 34
        }, 
        "confidence": 0.17681329, 
        "bottomright": {
            "y": 347, 
            "x": 398
        }, 
        "label": "person"
    }, 
    {
        "topleft": {
            "y": 48, 
            "x": 29
        }, 
        "confidence": 0.107129775, 
        "bottomright": {
            "y": 351, 
            "x": 399
        }, 
        "label": "car"
    }, 
    {
        "topleft": {
            "y": 85, 
            "x": 20
        }, 
        "confidence": 0.22963998, 
        "bottomright": {
            "y": 350, 
            "x": 376
        }, 
        "label": "cat"
    }, 
    {
        "topleft": {
            "y": 2, 
            "x": 0
        }, 
        "confidence": 0.12423642, 
        "bottomright": {
            "y": 356, 
            "x": 372
        }, 
        "label": "sheep"
    }, 
    {
        "topleft": {
            "y": 12, 
            "x": 20
        }, 
        "confidence": 0.26517922, 
        "bottomright": {
            "y": 349, 
            "x": 378
        }, 
        "label": "dog"
    }
]
lenik
  • 21,662
  • 4
  • 29
  • 38