Json Parse error in Ruby: `parse': 795: unexpected token

I am getting this Json parse error in Ruby. I searched online and
followed suggestions like gsub etc, but still the error persists. Any
help would be greatly appreciated.

/Library/Ruby/Gems/1.8/gems/json-1.8.3/lib/json/common.rb:155:in
parse': 757: unexpected token at '"{"cli_output": "Hello World"}" (JSON::ParserError) ' from /Library/Ruby/Gems/1.8/gems/json-1.8.3/lib/json/common.rb:155:inparse’
from deep3.rb:84

Code snippet :

  puts("Printing response before gsub:")
  print(response)
  puts("Printing response body:")
  print(response.body)
  body = response.body.gsub('\"', '"')
  puts("Printing body after gsub : ")
  print(body)
  body = JSON.parse(body)

Relevant output before Parse error is:

Printing response before gsub:
#Net::HTTPOK:0x10c1edae0
Printing response body:
“{“cli_output”: “Hello World”}”
Printing body after gsub :
“{“cli_output”: “Hello World”}”
/Library/Ruby/Gems/1.8/gems/json-1.8.3/lib/json/common.rb:155:in
parse': 757: unexpected token at '"{"cli_output": "Hello World"}" (JSON::ParserError) ' from /Library/Ruby/Gems/1.8/gems/json-1.8.3/lib/json/common.rb:155:inparse’
from deep3.rb:84

It seems something went wrong earlier in the script when you got the
data via http? What’s the code and where are you getting the json from?

The following code works as expected for me:

require ‘net/http’
require ‘json’
response=Net::HTTP.get_response(URI(‘https://api.github.com/users/mralexgray/repos’))
data = JSON.parse(response.body)
puts data.class # => Array

The JSON data you posted resembles what one would get after calling the
String#inspect method on the real JSON string:

require ‘json’
a=’{“cli_output”: “Hello World”}’
b=a.inspect
puts a # => {“cli_output”: “Hello World”}
puts b # => “{“cli_output”: “Hello World”}”
JSON.parse(a) # works
JSON.parse(b)

JSON::ParserError: 757: unexpected token at ‘"{“cli_output”: “Hello
World”}"’
from
./.rvm/gems/[email protected]/gems/json-1.8.3/lib/json/common.rb:155:in
parse' from ./.rvm/gems/[email protected]/gems/json-1.8.3/lib/json/common.rb:155:inparse’
from (irb):18
from ./.rvm/rubies/ruby-2.2.1/bin/irb:11:in `’

res = “Hello World”
myreqJson = ‘{“cli_output”: "’…res…’"}’
ngx.say(cjson.encode(myreqJson))

You are converting a string to JSON here. Ruby’s parser does not
support top-level strings (without quirk mode), and at any rate, this is
probably not what you want. What you want to do is
stringify the object

myreqJson = {“cli_output”: res}

as JSON.

value = { true, { foo = “bar” } }
json_text = cjson.encode(value)

http://www.kyne.com.au/~mark/software/lua-cjson-manual.html

Thanks Dansei !

Full script is like this :

  @http = Net::HTTP.new('10.1.2.3')
  request.body = {'cli' => 'show mystring', 'type' =>

‘exec’}.to_json
puts(“Printing request : “)
print(request)
puts(“Printing request body : “)
print(request.body)
puts(“Sending HTTP request to NX-API at #{@http.address}:\n”)
response = @http.request(request)
puts(”\n Got response”)
puts(response.body)
puts(“Printing response before gsub:”)
print(response)
puts(“Printing response body:”)
print(response.body)
body = response.body.gsub(’”’, '”’)
puts("Printing body after gsub : ")
print(body)
body = JSON.parse(body)

Json response is constructed by the NGINX server running a lua script
using cjson :

  res = "Hello World"
  myreqJson = '{"cli_output": "'..res..'"}'
  ngx.say(cjson.encode(myreqJson))

Output & error:

Printing request :
#Net::HTTP::Post:0x10a2492e8
Printing request body :
{“type”:“exec”,“cli”:“show mystring”}Sending HTTP request to NX-API at
10.1.2.3:
Got response
“{“cli_output”: “Hello World”}”
Printing response before gsub:
#Net::HTTPOK:0x10a246bb0
Printing response body:
“{“cli_output”: “Hello World”}”
Printing body after gsub :
“{“cli_output”: “Hello World”}”
/Library/Ruby/Gems/1.8/gems/json-1.8.3/lib/json/common.rb:155:in
parse': 757: unexpected token at '"{"cli_output": "Hello World"}" (JSON::ParserError) ' from /Library/Ruby/Gems/1.8/gems/json-1.8.3/lib/json/common.rb:155:inparse’
from deep3.rb:84

Dansei Yuuki wrote in post #1181118:

It seems something went wrong earlier in the script when you got the
data via http? What’s the code and where are you getting the json from?

The following code works as expected for me:

require ‘net/http’
require ‘json’

response=Net::HTTP.get_response(URI(‘https://api.github.com/users/mralexgray/repos’))

data = JSON.parse(response.body)
puts data.class # => Array

The JSON data you posted resembles what one would get after calling the
String#inspect method on the real JSON string:

require ‘json’
a=’{“cli_output”: “Hello World”}’
b=a.inspect
puts a # => {“cli_output”: “Hello World”}
puts b # => “{“cli_output”: “Hello World”}”
JSON.parse(a) # works
JSON.parse(b)

JSON::ParserError: 757: unexpected token at ‘"{“cli_output”: “Hello
World”}"’
from
./.rvm/gems/[email protected]/gems/json-1.8.3/lib/json/common.rb:155:in
parse' from ./.rvm/gems/[email protected]/gems/json-1.8.3/lib/json/common.rb:155:inparse’
from (irb):18
from ./.rvm/rubies/ruby-2.2.1/bin/irb:11:in `’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs