Parse Array of JSON Objects in Crystal lang
Asked Answered
M

1

5

Suppose I've got a simple JSON mapped object in Crystal lang, e.g.:

class Item
  JSON.mapping(
    id: UInt32,
    name: String,
  )
end

I can parse individual objects from JSON strings easily like so:

foo = Item.from_json(%({"id":1,"name":"Foo"}))
puts "OK: foo=#{foo}"
#  => OK: foo=Item(@id=1, @name="Foo")

But how would I parse an array of Items from a JSON string? I've tried a few approaches but am not sure how to proceed, e.g.:

items_str = %([{"id":1,"name":"Foo"},{"id":2,"name":"Bar"}])
items : Array(Item) = JSON.parse(items_str)
# => Error in foo.cr:15: type must be Array(Item), not JSON::Any

Of course, I'd also like to be able to do this with a JSON pull parser, so presumably there's some mapping trick or type hint I'm missing. Ideas?

Main answered 15/9, 2017 at 19:50 Comment(0)
S
9

Found it in this spec. So, you can use Array(Item).from_json:

items = Array(Item).from_json %([{"id":1,"name":"Foo"},{"id":2,"name":"Bar"}])

items.first.id   #=> 1
items.first.name #=> "Foo"
items.last.id    #=> 2
items.last.name  #=> "Bar"
Sweyn answered 15/9, 2017 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.