{"id":26573,"date":"2022-12-18T17:15:14","date_gmt":"2022-12-18T11:45:14","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/"},"modified":"2022-12-18T17:15:14","modified_gmt":"2022-12-18T11:45:14","slug":"solved-regular-expression-to-split-ruby-strings-into-multiple-hashes","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/","title":{"rendered":"[Solved] Regular expression to split ruby strings into multiple hashes"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-44034333\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"44034333\" data-parentid=\"44023307\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>It looks like you tried to create your own JSON serializer, instead of using the one that comes with Ruby. Or, perhaps you didn&#8217;t realize that JSON was a thing and wanted to store arrays of hashes and tried to invent your own. Either way, it wasn&#8217;t a good path to follow.<\/p>\n<p>This is your definition, which, after being retrieved would remain a String. It appears to be the elements of an Array of Hashes, without the surrounding Array <code>[<\/code> and <code>]<\/code>:<\/p>\n<pre><code>foo = \"{\\\"monday\\\"=&gt;\\\"{:start_time=&gt;9.0, :end_time=&gt;10.5}\\\"},{\\\"tuesday\\\"=&gt;\\\"{:start_time=&gt;8.5, :end_time=&gt;10.0}\\\"},{\\\"wednesday\\\"=&gt;\\\"{:start_time=&gt;8.5, :end_time=&gt;10.0}\\\"}\"\n<\/code><\/pre>\n<p>and it remains a String:<\/p>\n<pre><code>foo.class # =&gt; String\n<\/code><\/pre>\n<p>It&#8217;d be possible to munge it back into a form that could be evaluated and turned back into an Array of Hashes, but it&#8217;s not straightforward, nor is it clean or convenient or elegant.<\/p>\n<p>I suspect your original object looked like this:<\/p>\n<pre><code>foo = {\"monday\"=&gt;{:start_time=&gt;9.0, :end_time=&gt;10.5}},{\"tuesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}},{\"wednesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}}\n# =&gt; [{\"monday\"=&gt;{:start_time=&gt;9.0, :end_time=&gt;10.5}},\n#     {\"tuesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}},\n#     {\"wednesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}}]\n<\/code><\/pre>\n<p>which is an Array:<\/p>\n<pre><code>foo.class # =&gt; Array\n<\/code><\/pre>\n<p>It&#8217;s an Array now because you have multiple Hash elements separated by commas (<code>,<\/code>) which Ruby interprets as an Array when assigned to a single variable. This would be a less visually confusing example:<\/p>\n<pre><code>foo = {a: 1}, {a: 2} # =&gt; [{:a=&gt;1}, {:a=&gt;2}]\nfoo.class  # =&gt; Array\nfoo.first.class # =&gt; Hash\n<\/code><\/pre>\n<p>Back to the Array of Hashes in <code>foo<\/code>: Starting from that it&#8217;s easy to let the JSON class serialize it into a string that can be stored in a database, then retrieved and reparsed back into a Ruby object:<\/p>\n<pre><code>require 'json'\nfoo.to_json\n<\/code><\/pre>\n<p><code>foo.to_json<\/code> would result in a serialized string that looks like:<\/p>\n<pre><code>[{\"monday\":{\"start_time\":9.0,\"end_time\":10.5}},{\"tuesday\":{\"start_time\":8.5,\"end_time\":10.0}},{\"wednesday\":{\"start_time\":8.5,\"end_time\":10.0}}]\n<\/code><\/pre>\n<p>And, given that string, JSON can rebuild the object:<\/p>\n<pre><code>bar = JSON.parse(foo.to_json)\n# =&gt; [{\"monday\"=&gt;{\"start_time\"=&gt;9.0, \"end_time\"=&gt;10.5}},\n#     {\"tuesday\"=&gt;{\"start_time\"=&gt;8.5, \"end_time\"=&gt;10.0}},\n#     {\"wednesday\"=&gt;{\"start_time\"=&gt;8.5, \"end_time\"=&gt;10.0}}]\n<\/code><\/pre>\n<p>Note that it&#8217;s not necessary to use <code>parse<\/code> or <code>to_json<\/code>. The class is smart enough to recognize whether the parameter is a String, or an Array or Hash, and parse or serialize respectively:<\/p>\n<pre><code>JSON[JSON[foo]]\n# =&gt; [{\"monday\"=&gt;{\"start_time\"=&gt;9.0, \"end_time\"=&gt;10.5}},\n#     {\"tuesday\"=&gt;{\"start_time\"=&gt;8.5, \"end_time\"=&gt;10.0}},\n#     {\"wednesday\"=&gt;{\"start_time\"=&gt;8.5, \"end_time\"=&gt;10.0}}]\n<\/code><\/pre>\n<p>At this point you&#8217;re prepared to store the values into a String-type field in your database, then retrieve them later and reuse them.<\/p>\n<p>If you are hoping to search or manipulate those stored JSON strings, I&#8217;d recommend re-thinking that. While a modern DBM can search inside JSON and generate it, you&#8217;d be better off having a separate table of the keys and values. It&#8217;s faster and more flexible.<\/p>\n<p>Finally to your question, how to convert the string so you can get the keys and values:<\/p>\n<pre><code>foo = \"{\\\"monday\\\"=&gt;\\\"{:start_time=&gt;9.0, :end_time=&gt;10.5}\\\"},{\\\"tuesday\\\"=&gt;\\\"{:start_time=&gt;8.5, :end_time=&gt;10.0}\\\"},{\\\"wednesday\\\"=&gt;\\\"{:start_time=&gt;8.5, :end_time=&gt;10.0}\\\"}\"\nbar = eval('[' + foo.gsub(\/\"([{}])\/, '\\1') + ']')\nbar\n# =&gt; [{\"monday\"=&gt;{:start_time=&gt;9.0, :end_time=&gt;10.5}},\n#     {\"tuesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}},\n#     {\"wednesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}}]\n\nbar.map { |h| [h.keys, h.values.first.keys] }\n# =&gt; [[[\"monday\"], [:start_time, :end_time]],\n#     [[\"tuesday\"], [:start_time, :end_time]],\n#     [[\"wednesday\"], [:start_time, :end_time]]]\n<\/code><\/pre>\n<p>This hints that the Array of Hashes wasn&#8217;t the right structure either. A simple hash would have sufficed:<\/p>\n<pre><code>foo = {\n  \"monday\"=&gt;{:start_time=&gt;9.0, :end_time=&gt;10.5},\n  \"tuesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0},\n  \"wednesday\"=&gt;{:start_time=&gt;8.5, :end_time=&gt;10.0}\n}\n\nrequire 'json'\nbar = JSON[JSON[foo]] # imitate a round-trip to\/from the DB\nbar.keys # =&gt; [\"monday\", \"tuesday\", \"wednesday\"]\nbar.values # =&gt; [{\"start_time\"=&gt;9.0, \"end_time\"=&gt;10.5}, {\"start_time\"=&gt;8.5, \"end_time\"=&gt;10.0}, {\"start_time\"=&gt;8.5, \"end_time\"=&gt;10.0}]\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Regular expression to split ruby strings into multiple hashes <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] It looks like you tried to create your own JSON serializer, instead of using the one that comes with Ruby. Or, perhaps you didn&#8217;t realize that JSON was a thing and wanted to store arrays of hashes and tried to invent your own. Either way, it wasn&#8217;t a good path to follow. This is &#8230; <a title=\"[Solved] Regular expression to split ruby strings into multiple hashes\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\" aria-label=\"More on [Solved] Regular expression to split ruby strings into multiple hashes\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[623,347,455,615],"class_list":["post-26573","post","type-post","status-publish","format-standard","hentry","category-solved","tag-postgresql","tag-regex","tag-ruby","tag-ruby-on-rails"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Regular expression to split ruby strings into multiple hashes - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Regular expression to split ruby strings into multiple hashes - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] It looks like you tried to create your own JSON serializer, instead of using the one that comes with Ruby. Or, perhaps you didn&#8217;t realize that JSON was a thing and wanted to store arrays of hashes and tried to invent your own. Either way, it wasn&#8217;t a good path to follow. This is ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-18T11:45:14+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Regular expression to split ruby strings into multiple hashes\",\"datePublished\":\"2022-12-18T11:45:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\"},\"wordCount\":388,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"postgresql\",\"regex\",\"ruby\",\"ruby-on-rails\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\",\"name\":\"[Solved] Regular expression to split ruby strings into multiple hashes - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-18T11:45:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Regular expression to split ruby strings into multiple hashes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Regular expression to split ruby strings into multiple hashes - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Regular expression to split ruby strings into multiple hashes - JassWeb","og_description":"[ad_1] It looks like you tried to create your own JSON serializer, instead of using the one that comes with Ruby. Or, perhaps you didn&#8217;t realize that JSON was a thing and wanted to store arrays of hashes and tried to invent your own. Either way, it wasn&#8217;t a good path to follow. This is ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/","og_site_name":"JassWeb","article_published_time":"2022-12-18T11:45:14+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Regular expression to split ruby strings into multiple hashes","datePublished":"2022-12-18T11:45:14+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/"},"wordCount":388,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["postgresql","regex","ruby","ruby-on-rails"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/","url":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/","name":"[Solved] Regular expression to split ruby strings into multiple hashes - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-18T11:45:14+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-regular-expression-to-split-ruby-strings-into-multiple-hashes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Regular expression to split ruby strings into multiple hashes"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26573"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26573\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}