2

I'm using the Locu API and I've currently got the results from an HTTParty request into a ruby hash called data.

I've tried to call data.to_json, and this puts backslashes in my hash:

data.to_json
 => "{\"meta\":{\"limit\":25,\"cache-expiry\":3600}

Somebody else posted the same problem that was solved as follows:

puts data.to_json

Unfortunately, that does not alter my hash at all. Any ideas?

4
  • I assume, what you need to do is JSON.parse(data.to_json) Commented Sep 11, 2015 at 3:28
  • That is exactly what I need to do but for some reason it is not parsing my JSON. It's not raising an error either--just returning the same hash. Commented Sep 11, 2015 at 3:47
  • to_json serializes (converts) data into a string JSON, so its working as expected. What are you trying to do with the json? Render it in a controller? Import it into the database? What's not working, exactly? Commented Sep 11, 2015 at 3:49
  • I want to be able to read it clearly in my console, so I can begin to add methods in my model with different endpoints from the API. Right now, I can't read any of it, because it is one large block. I thought turning it into JSON and parsing it would make it readable, so that I can begin to get more of a sense of what I am working with. Commented Sep 11, 2015 at 3:53

3 Answers 3

8

I've tried to call data.to_json, and this puts backslashes in my hash:

Two things: The output of that is not a hash. It's a string representing your Hash in JSON. Second, the backslashes are ok. It's only to disambiguate the double-quotes from the leading and trailing double quotes.

Instead of

data.to_json

do

puts data.to_json

Which will print as you expected:

 => {"meta":{"limit":25,"cache-expiry":3600}
Sign up to request clarification or add additional context in comments.

10 Comments

I guess maybe I'm asking the wrong question. Right now, my JSON is all in one large block, but I want it be able to parse it. However, it is not responding to JSON.parse(data).
JSON is your data represented in a string. You are converting your hash to a string by doing data.to_json. If you want to parse it, you are converting a string to a hash. But why are you doing that when you already have data in hash form?
Because right now it is just one huge block of text and it's nearly impossible for me to begin to read the different keys and values. I want to parse it to more easily access and visualize the data and get a sense of what my hash actually looks like.
you can do require 'pp' and then pp data
That still returns a hash for some reason.
|
1

It is working as intended.

What you've done is serialize a hash into a JSON String, used for passing data around via HTTP or something similar.

2 Comments

How can I turn it into a JSON object?
So JSON isn't really an object, it's a notation. You can read more about it here. I think a better question would be what are you wanting to do with the JSON? That will help me help you.
0

That's a serialized string so you will need to run

JSON.parse(data)

in order to convert it to JSON

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.