Nesting too deep' error while retrieving JSON using HTTParty
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
Nesting error when trying to parse JSONfile
Try setting the max_nesting value:
result = JSON.parse(@response, :max_nesting => 100)
HTTParty.get with callback causes MultiJson::DecodeError.
The problem is that the response is javascript, not JSON. To fix it you can do:
response.gsub! /^\d+\((.*)\);?$/m, '\\1'
or something similar.
set json max_nesting option from within Ruby on Rails application
ActiveSupport::JSON
uses the multi-json gem for encoding and decoding operations. The multi-json gem supports a variety of engines, and supported options will differ between them.
You can check which engine you are using by running
require 'multi_json'
puts MultiJson.engine
Mine was MultiJson::Adapters::Yajl
but other options are possible as well. Multi-json doesn't seem to pass options to each engine in the same way, so I'd recommend using the JSON-gem directly.
If you're using the json-gem, you could just skip the ActiveSupport-chain and parse your data using JSON.parse, to which you can pass the :max_nesting
option directly.
Why do Ι get this error when trying to scrape a reddit Json file?
My guess is it's the off by one error.
a = [1, 2, 3, 4, 5]
(0..a.length).map { |n| a[n] }
# => [1, 2, 3, 4, 5, nil]
This is because
(0..5).to_a
# => [0, 1, 2, 3, 4, 5]
To be non-inclusive, you should use ...
, that is:
(0...comments.length).each do |i|
Better yet, since comments is an array, you can do:
comments.each do |comment|
puts comment["data"]["author"]
end
Related Topics
How to Listen to Stdin Input Without Pausing My Script
Is There a More Concise Way to Call an Outside Method on a Map in Ruby
What Is the Equivalent to Rspec's 'It "Should …", Focus: True Do' in Minitest/Spec
Detect Similar Sounding Words in Ruby
How to Pass Content to Jekyll Default Converter After Custom Conversion
Unable to Delete File from Amazon S3 Using Ruby Script
Using Ruby and Mechanize to Fill in a Remote Login Form Mystery
Algorithm to Print All Valid Combations of N Pairs of Parenthesis
Finding the Date for a Given Week Number
Ruby Defining Operator Procedure
Ruby's Argv Can Be Empty on Windows Depending on a Way to Run Script
What Regex How to Use to Get the Domain Name from a Url in Ruby