# How to do 'from\_json' with Symbol variables?

**URL:** <https://forum.crystal-lang.org/t/how-to-do-from-json-with-symbol-variables/3379>\
**Category:** Offtopic\
**Created:** [June 13, 2021, 3:43am UTC](https://forum.crystal-lang.org/t/how-to-do-from-json-with-symbol-variables/3379 "2021-06-13T03:43:14Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 13, 2021, 3:58am UTC](https://forum.crystal-lang.org/t/how-to-do-from-json-with-symbol-variables/3379/2 "2021-06-13T03:58:18Z")

</div>

> [@drhuffman12](#):
>
> (a) How do I to do ‘from\_json’ on a class that has a property of type Symbol?

You can’t dynamically create a `Symbol`, so there’s not really a way to do this. Have you considered using an `Enum`? E.g. something like:

```auto
enum State
  FOO
  BAR
end

class Foo
  include JSON::Serializable
 
  property state : State
 
  def initialize(@state : State)
  end
end

f = Foo.new(state: :bar)
p! f # => #<Foo:0x7f124d85eeb0 @state=BAR>
p! f.to_json # => "{\"state\":\"bar\"}"
p! Foo.from_json(f.to_json) # => #<Foo:0x7f124d85ee90 @state=BAR>

```

> [@drhuffman12](#):
>
> (b) Should a Symbol loose or keep the `:` when converted to JSON?

It should definitely lose it. `JSON` doesn’t have the concept of a `Symbol`. The closest representation is `String`, hence why it loses the `:`.

---

_[View the full topic](https://forum.crystal-lang.org/t/how-to-do-from-json-with-symbol-variables/3379)._
