'Nesting too deep' error while retrieving JSON using HTTParty

放肆的年华 提交于 2019-12-01 06:15:19

It's not that the JSON is too large. It's nested too deeply. HTTParty tries to decode the results that it gets automatically. Following your stack trace, and the HTTParty dependencies, it relies on multi_json which is using the json gem.

Inside of json there is lib/json/pure/parser.rb. The default max depth set is set in there, specifically on line 79. Something in your returned JSON is 20+ levels deep, triggering the exception.

 if !opts.key?(:max_nesting) # defaults to 19
   @max_nesting = 19

It looks like this can be worked around by calling to_json with the max_nesting option set.

json = obj.to_json(max_nesting: 50)

Switched to the OJ JSON parser by adding the following to my Gemfile:

gem 'oj'

and this issue resolved itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!