1

What is the cleanest way to perform nested conversion of a "deep" object that contains mixed python / numpy types to an object containing only python types?

The question is motivated by the need to send the data as JSON, but here I do not have control over json.dumps() because that is the province of a different application. In other words, I cannot specify the JSON encoder.

One possible solution involves adopting the JSON encoder solution anyway, followed by a conversion back to JSON with json.loads(). This would mean every message has two round trips to JSON rather than one which might not be the end of the way. But is there a "better" alternative?

Note that I need to apply recursively so that fact that tolist() or item() sometimes works isn't a complete solution here.

Peter Cotton
  • 1,671
  • 14
  • 17
  • Arrays have an efficient `tolist` method. Of course it's most useful for numeric dtypes. It doesn't do anything to elements of object dtype arrays. – hpaulj Nov 10 '21 at 22:46
  • 1
    From the question you linked, I find [this answer](https://stackoverflow.com/a/52604722/4601890) the best, most clean – dankal444 Nov 12 '21 at 21:41
  • The issue though is that you still have to walk the object no? – Peter Cotton Nov 13 '21 at 17:10

0 Answers0