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
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:
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
record FirstAndLast, first : String, last : String do
include JSON::Serializable
end
FirstAndLast.from_json(string_output).first # => "Alpha"