Is it safe to parse json with YAML.load?
json, ruby, yaml
Solution
YAML can load JSON
YAML.load('{"something": "test", "other": 4 }')
=> {"something"=>"test", "other"=>4}
JSON will not be able to load YAML.
JSON.load("- something\n")
JSON::ParserError: 795: unexpected token at '- something'
There will be some obscure cases that work and produce different output.
YAML.load("")
=> false
JSON.load("")
=> nil
But generally the YAML construct is not JSON compliant.
So, try the `JSON.load` first because it's probably better at obscure JSON things. Catch the `JSON::ParserError` error and fall back to `YAML.load`.
Problem
I am using ruby 2.1.0 I have a json file. For example: test.json ``` { "item":[ {"apple": 1}, {"banana": 2} ] } ``` Is it safe to load this file with YAML.load? ``` YAML.load(File.read('test.json')) ``` I am trying to load a file which is in either json or yaml format.