1

I read json file after load it into json but i got error JSON::ParserError unexpected token at, i couldn't json parse.Below i mentioned what i got output after file read

Here my code,

file = File.read("sample.json") 
hash = JSON.load(file)

after read my json file,

"{\"rename\"=>[{\"from\"=>\"TTTC\", \"to\"=>\"AAAB\"}, {\"from\"=>\"AAAA\", \"to\"=>\"Description\"}, {\"from\"=>\"AAAC\", \"to\"=>\"test\"}], \"drop\"=>{\"fields\"=>[\"AAAG\", \"AAAH\"]}}"

3 Answers 3

2

This is not valid in json =>. JSON looks like

{ "rename": [{ "from": "TTTC" }] }
Sign up to request clarification or add additional context in comments.

Comments

2

That's not JSON that's a string created by applying the inspect method to a hash.

You can convert it back to a hash with eval

hash = eval(file)

However eval can be a security hole, so you should only do this if you're confident about the source and contents of the file.

Comments

0

If you're encountering this error while testing - make sure to add .to_json to your request's body hash.

Example:

headers = {
  'ACCEPTS' => 'application/json'
}

post '/api-endpoint-here', { name: 'Dylan Pierce' }.to_json, headers

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.