1

Currently my app is parsing JSON in following way:

OpenStruct.new(JSON.parse(json_string))

but there are problems with this approach:

  1. JSON allows root-level arrays like [{id: 1, name: "first"}, {id: 2, name: "second"}] and OpenStruct can't handle them throwing an exception.
  2. OpenStruct does not handle nested elements.

I found some solutions like RecursiveOpenStruct and others.

  1. They handle nested hashes, but still can't handle top-level arrays
  2. Hashes nested into array remain hashes: {data: [{id: 1, name: "first"}, {id: 2, name: "second"}]} here each .data[i] is a Hash.
Paul
  • 25,812
  • 38
  • 124
  • 247

0 Answers0