LoginSignup
11
6

More than 5 years have passed since last update.

jsonのパースで出たエラーの解決方法 lib/ruby/2.1.0/json/common.rb:155:in `parse': 795: unexpected token at ': [{ (JSON::ParserError)

Last updated at Posted at 2016-04-20

初めてjsonをパースしてみましたが、エラーが出てしまいました。

/Users/home/.rbenv/versions/2.1.4/lib/ruby/2.1.0/json/common.rb:155:in `parse': 795: unexpected token at ': [{ (JSON::ParserError)

コードはこんな感じです。

require 'json'

json_file = File.open('test.json').read
hash = JSON.load(json_file)
puts hash

今回のjsonファイルはWebサイトから切り出してきたものだったので、ファイルが悪いのかコードが悪いのかチェックするためにjsonファイルを取得できるサイトを見つけてHTTP通信で取得してみました。

require 'json'
require 'open-uri'

json_file = open('https://hogehoge/hoge').read
hash = JSON.load(json_file)
puts hash

こっちは問題なし!切り出し方法が悪いということでチェックしてくれるツールを探した所、発見。

JSONLint

JSONファイルをコピペするとエラー箇所を教えてくれます。
今回はダブルクオーテーションで囲われていない文字列がエラーを出してました。

11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6