Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Array of JSON Objects in Crystal lang

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?

like image 895
maerics Avatar asked Mar 09 '23 01:03

maerics


1 Answers

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"
like image 186
Vitalii Elenhaupt Avatar answered Mar 10 '23 17:03

Vitalii Elenhaupt