1

I have an output from an API that look like this... (its a string)

[[2121212,212121,asd],[2323232,23232323,qasdasd]]

Its a string - not an array. I want to convert it to an array and then extract the first two elements in each array in the nested array to:

[2121212,212121],[2323232,23232323]

What's the best way to do this ruby? I could use regexp and extract - but basically the string is already an array, however the class is a string.

I tried

array.push(response)

but that just put the string in to the array as one element. I guess what would be nice is a to_array method

3
  • possible duplicate of Ruby: Parsing a string representation of nested arrays into an Array? Commented Apr 23, 2012 at 9:32
  • This is not the same - neither JSON nor eval will handle strings without quotation marks. If the strings are so simple as asd, I'd just added quotations marks to them by regexp and then used JSON.parse or eval: eval str.gsub(/([a-zA-Z]+)/, '"\1"') Commented Apr 23, 2012 at 9:37
  • Lukas - sorry the strings will change - they will always be syntactically the same ie [element, element,element] I could call a .to_json method on it and have it return as "[element, element, element'] Commented Apr 23, 2012 at 9:41

3 Answers 3

2

You will need to use regular expression anyway if not eval (shrudder...), this is the shortest one

str = "[[2121212,212121,asd],[2323232,23232323,qasdasd],[2424242,24242424,qasdasd]]"
p str.scan(/(\d+),(\d+)/)

=>[["2121212", "212121"], ["2323232", "23232323"], ["2424242", "24242424"]]
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming this is a JSON response (and if so, it is badly malformed and you should talk to the people that are responsible for this) you could write something like:

require 'json'

input= '[[2121212,212121,Asd],[2323232,23232323,qasdasd]]'
input.gsub!(/([A-Za-z ]+)/,'"\1"')
json = JSON.parse input
output = json.map{|x| x[0...2]}
p output

this prints

[[2121212, 212121], [2323232, 23232323]]

1 Comment

if your code also parses valid json, you should improve the regex in such a manner that it will not quote already quoted strings, though. but i'll leave that up to you :)
0

Using eval is very bad but I have no other easy option.

test_str = "[[2121212,212121,asd],[2323232,23232323,qasdasd]]"
test_str.gsub!(/([a-z]+)/) do
  "'#{$1}'" 
 end
 => "[[2121212,212121,'asd'],[2323232,23232323,'qasdasd']]"
test_array = eval(test_str)
 => [[2121212, 212121, "asd"], [2323232, 23232323, "qasdasd"]]
test_array.each do |element|
  element.delete(element.last)
end
 => [[2121212, 212121], [2323232, 23232323]]

3 Comments

You definitely has other options than eval. Namely JSON.parse. Also, your gsub can be written without the block: str.gsub!(/([a-z]+)/', '"\\1"').
Thanks for your suggestion but JSON.parse("[[2121212,212121,asd],[2323232,23232323,qasdasd]]") JSON::ParserError: 399: unexpected token at 'asd],[2323232,23232323,qasdasd]]'
@soundar: but the thing you are evalling could be just as easily JSON.parsed if you only changed the line "'#{$1}'" to "\"#$1\"". You would then produce something like '[[21212,212,"asd"],…]', which is valid JSON.

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.