1

I'm trying to parse a string in ruby to a hash but I can't quite figure it out. I've gotten it to a nested array just need to map it to the hash.

example string

subject = "{\"CN\"=\"schoen.io\", \"C\"=\"US\", \"ST\"=\"Texas\", \"L\"=\"North Doyle\", \"O\"=\"SSL Corporation\", \"OU\"=\"Information Technology Department\", \"2.5.4.17\"=\"16039-4645\", \"2.5.4.9\"=\"8268 Kemmer Village\", \"2.5.4.42\"=\"Tracy\", \"2.5.4.4\"=\"Jacobi\", \"2.5.4.5\"=\"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx\", \"2.5.29.17\"=\"ssl.com\"}}"

desired hash

{'CN' => 'schoen.io', 'C' => 'US', 'ST' => 'Texas',... }

my code

subject.gsub('{','').gsub('}','').split(',').map { |m| m.split('=')}

generated array

[["\"CN\"", "\"schoen.io\""], [" \"C\"", "\"US\""], [" \"ST\"", "\"Texas\""], [" \"L\"", "\"North Doyle\""], [" \"O\"", "\"SSL Corporation\""], [" \"OU\"", "\"Information Technology Department\""], [" \"2.5.4.17\"", "\"16039-4645\""], [" \"2.5.4.9\"", "\"8268 Kemmer Village\""], [" \"2.5.4.42\"", "\"Tracy\""], [" \"2.5.4.4\"", "\"Jacobi\""], [" \"2.5.4.5\"", "\"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx\""], [" \"2.5.29.17\"", "\"ssl.com\""]]
1
  • The first question to be asked though: WHY do you have to deal with a non-standardised structure like this? Could it be produced in JSON instead? Commented May 18, 2020 at 17:49

1 Answer 1

5

I think you have a typo in you original subject. You have two } at the end of your string and only one at the beginning.

If you remove it your string is now :

subject = "{\"CN\"=\"schoen.io\", \"C\"=\"US\", \"ST\"=\"Texas\", \"L\"=\"North Doyle\", \"O\"=\"SSL Corporation\", \"OU\"=\"Information Technology Department\", \"2.5.4.17\"=\"16039-4645\", \"2.5.4.9\"=\"8268 Kemmer Village\", \"2.5.4.42\"=\"Tracy\", \"2.5.4.4\"=\"Jacobi\", \"2.5.4.5\"=\"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx\", \"2.5.29.17\"=\"ssl.com\"}"

You just need to do : JSON.parse(subject.gsub('=',':'))

And you will get the desired output :

{
 "CN"=>"schoen.io",
 "C"=>"US",
 "ST"=>"Texas",
 "L"=>"North Doyle",
 "O"=>"SSL Corporation",
 "OU"=>"Information Technology Department",
 "2.5.4.17"=>"16039-4645",
 "2.5.4.9"=>"8268 Kemmer Village",
 "2.5.4.42"=>"Tracy",
 "2.5.4.4"=>"Jacobi",
 "2.5.4.5"=>"grbh52f84senk4jkgo9n9a66yg62w78y4a0v36ax8tfacdshublxjpq6arcn7qyx",
 "2.5.29.17"=>"ssl.com"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Danger here, = could be part of the value, like "KEY"="some=value"
Indeed but since the OP used m.split('=') in the question, I assumed it was safe to use.
If you want to allow an = inside string context you could use something like subject.scan(/(?:"(?:\\"|[^"])*"|[^=])+/).join(':')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.