5

I have a javascript object of this form

 obj = "[
   {
     title: "Sean Kingston1",
     duration: parseInt("71", 10),
   },
   {
     title: "Sean Kingston2",
     duration: parseInt("71", 10),
   },
 ]"

is there a way to convert this to a ruby hash ?

I tried using JSON.parse and JSON.load
both of them throw

 JSON::ParserError: lexical error: invalid string in json text.
                               {   title: "Sean Kingston1
                 (right here) ------^

Is there a generic solution or should I use regex and then construct the hash in ruby ?

7
  • 1
    Deleting your old question and creating a new one doesn't change the fact that what you have is not valid JSON. Commented Mar 6, 2012 at 8:43
  • 2
    @andrew i did change the question its a javascript object and that is what i get from the server... do u know an answer to the question ?? Commented Mar 6, 2012 at 9:10
  • @andrew i never used the word JSON! Commented Mar 6, 2012 at 9:13
  • 1
    You did when you tagged it as JSON. Commented Mar 6, 2012 at 9:14
  • my bad,i didnt notice! what do u think i shld do here ? i will probably use reqex and convert all the title and duration to "title" and "duration".. is there a generic solution ? Commented Mar 6, 2012 at 9:19

2 Answers 2

4

ruby 1.9 supports hash of this kind which resembles a javascript object

 obj = "[
  {
   title: "Sean Kingston1",
   duration: "71",
  },
  {
   title: "Sean Kingston2",
   duration: "71",
  },
 ]"

to convert this into a ruby hash

 eval(obj)
Sign up to request clarification or add additional context in comments.

5 Comments

Actually you may need to have numbers not quoted. At least this is allowed even in JSON (which has even more strictly syntax)
@kirilloid yup i will do it right away, thank u :) actually i get this Js object by scraping a site and my scraper is written in ruby!
Be careful when evaling code from random websites!
I found my own answer when I was trying to build a Ruby Map out of this data a0.awsstatic.com/pricing/1/ec2/linux-od.min.js :-)
2

This is not a JSON. Actually, JSON is not the same as code, could be interpreted by javascript and evaluated to object.

JSON itself allows only static values (no parseInt) and any keys should be quoted as well.

[{
    "title": "Sean Kingston1",
    "duration": 71
 },
 {
    "title": "Sean Kingston2",
    "duration": 71
 }]

Using regexes and such things ain't good. You'd better just format JSON properly.

Ok, if you're not able to modify that input, you may solve a problem for this particular input with following regexpes:

/^\s*(\w+)\s*:/, '"\1":';
/:\s*parseInt\("(\d+)"\,\s*10)/, ': \1';

but for any variation in input you'll need to add more and more regexpes.

Generally, in order to interpret javascript you need to ... interpret javascript.

This is possible via installing some js Engine, like Rhino or V8 and binding it to Ruby.

7 Comments

the javascript code what i have is not under my control :( i am scraping a site..
i was very careful to not to use the word JSON , i have a javascript object of that kind.. now how do i convert it to hash ? is their a solution or shld i use regex ??
Ideally, in order to parse such code, you need to have full javascript interpreter in order to evaluate any input into objects, hashes or somewhat (I'm not familiar with Ruby).
i did something like this replaced parseInt("","") by the number and called eval(obj) it worked :)
Hmm. You may call eval for js code? Eval isn't good thing, but will work here.
|

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.