# String to named tuple

**URL:** <https://forum.crystal-lang.org/t/string-to-named-tuple/1545>\
**Category:** Help & Support\
**Created:** [December 27, 2019, 3:39am UTC](https://forum.crystal-lang.org/t/string-to-named-tuple/1545 "2019-12-27T03:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachinbhutani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sachinbhutani/32/666_2.png) [@sachinbhutani](https://forum.crystal-lang.org/u/sachinbhutani)\
**Post date:** [December 27, 2019, 3:39am UTC](https://forum.crystal-lang.org/t/string-to-named-tuple/1545/1 "2019-12-27T03:39:05Z")

</div>

this is newbie question, if this isn’t the right place to ask it, please re-direct me.

How do I convert a string containing valid json to a NamedTuple ?  
for example:  
string\_output\_from\_api = “{first: “Alpha”, last: “Beta”}”  
even though the sting contains a valid json,  
I was expecting function String.to\_json would remove the first char and last inverted comma characters and parse

when trying to create an object with the string, I get a runtime error  
crystal-playground example:  
[https://play.crystal-lang.org/#/r/8adh](https://play.crystal-lang.org/#/r/8adh)

---

<div class="post-metadata">

**Author:** ![lbarasti](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/lbarasti/32/648_2.png) [@lbarasti](https://forum.crystal-lang.org/u/lbarasti)\
**Post date:** [December 27, 2019, 12:34pm UTC](https://forum.crystal-lang.org/t/string-to-named-tuple/1545/2 "2019-12-27T12:34:53Z")

</div>

Calling `to_json` on a String will only turn it into a valid JSON string - escaping special characters like `"` and `\n`. Try comparing `string_output` and `string_output.to_json`.

In your script, `string_output` must be valid JSON already:

```auto
string_output = "{\"first\": \"Alpha\", \"last\": \"Beta\"}"

```

See how I added quotes to the object keys, and removed `to_json`, as that would add unwanted escape characters.

To clarify, your `string_output_from_api` does _not_ contain valid JSON.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [December 27, 2019, 12:50pm UTC](https://forum.crystal-lang.org/t/string-to-named-tuple/1545/3 "2019-12-27T12:50:10Z")

</div>

Once you _do_ have a string with valid JSON in it, you can use `NamedTuple(first: String, last: Beta).from_json string_output`. You could also create a custom struct with the expected fields like so

```crystal
record FirstAndLast, first : String, last : String do
  include JSON::Serializable
end
FirstAndLast.from_json(string_output).first # => "Alpha"

```

---

<div class="post-metadata">

**Author:** ![sachinbhutani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sachinbhutani/32/666_2.png) [@sachinbhutani](https://forum.crystal-lang.org/u/sachinbhutani)\
**Post date:** [December 27, 2019, 2:02pm UTC](https://forum.crystal-lang.org/t/string-to-named-tuple/1545/4 "2019-12-27T14:02:23Z")

</div>

thanks  
I figured i was trying to convert an existing json with .to\_json, which caused the issue

> Blockquote  
> response = HTTP::Client.get(“/api/me”)  
> string\_output = response.body.to\_json # this caused the issue  
> FirstAndLast.from\_json (string\_output) # parsing error

# instead I just needed to do

response = HTTP::Client.get(“/api/me”)  
FirstAndLast.from\_json (response.body)
