0

I am having trouble converting a string to a hash (hash with nested hashes actually) in javascript.

I want to convert the following string:

"{'btc_usd': {'price': 376.2, 'volume': 42812.69, 'change': -0.5},'btc_cny': {'price': 2519.39, 'volume': 67148.51, 'change': -85.13},'ltc_usd': {'price': 3.068, 'volume': 4735.55, 'change': -0.58},'btc_ltc': {'price': 0.00805433, 'volume': 153.33, 'change': -0.76},'btc_eth': {'price': 0.00660196, 'volume': 6428.98, 'change': 5.87}}"

I want to make it so that I can do hash['btc_usd']['price'] and get 376.2.

How can I do this?

This is what I have tried but it doesn't seem to be running:

var string="{'btc_usd': {'price': 376.2, 'volume': 42812.69, 'change': -0.5},'btc_cny': {'price': 2519.39, 'volume': 67148.51, 'change': -85.13},'ltc_usd': {'price': 3.068, 'volume': 4735.55, 'change': -0.58},'btc_ltc': {'price': 0.00805433, 'volume': 153.33, 'change': -0.76},'btc_eth': {'price': 0.00660196, 'volume': 6428.98, 'change': 5.87}}"
var results=JSON.parse(string);
4
  • 3
    That's not a valid JSON, where did you get it from? Commented Jan 30, 2016 at 19:28
  • 2
    To clarify, the single quotes ' should be double quotes " for it to be valid. Commented Jan 30, 2016 at 19:38
  • 1
    I'm pretty sure strings need to be in double quotes and single quotes isn't valid Commented Jan 30, 2016 at 19:43
  • Ok. Thank you for letting me know. I didn't it needed double quotes for it to be valid. Commented Jan 30, 2016 at 19:47

2 Answers 2

1

Why don't you use the JSON directly?

var string = "{'btc_usd': {'price': 376.2, 'volume': 42812.69, 'change': -0.5},'btc_cny': {'price': 2519.39, 'volume': 67148.51, 'change': -85.13},'ltc_usd': {'price': 3.068, 'volume': 4735.55, 'change': -0.58},'btc_ltc': {'price': 0.00805433, 'volume': 153.33, 'change': -0.76},'btc_eth': {'price': 0.00660196, 'volume': 6428.98, 'change': 5.87}}";
string = "hash = " + string + ";";
eval(string);

console.log(hash.btc_usd.price);

It is very simple but it comes with a price-tag: the eval() is dangerous if you do not know exactly where your string comes from, e.g.: you didn't produce it yourself. It is also expensive: if you want to use it in a loop over some thousand or more entries you'll see some time passing.

Sign up to request clarification or add additional context in comments.

Comments

1

The only thing that is different between your string and valid JSON is the usage of single quotes instead of double quotes. So you can just change that, and then parse the resulting JSON.

str = str.replace(/'/g, "\"");
var result = JSON.parse(str);

Of course this is only valid as long as there aren't string literals with single quotes (e.g. {'name': 'John O\'hara'}).

Comments

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.