Skip to content

Instantly share code, notes, and snippets.

@usahg
Last active December 15, 2015 02:19
Show Gist options
  • Save usahg/5186397 to your computer and use it in GitHub Desktop.
Save usahg/5186397 to your computer and use it in GitHub Desktop.
quotes nested values in json
# problem :
# was trying to import a 3D model in a web page in json format for use with threejs : https://github.com/mrdoob/three.js
# 3D model was exported from maya using this threejs plugin # https://github.com/mrdoob/three.js/tree/master/utils/exporters/maya
# exported json had two problems:
# 1. all of it was in one line, i.e no indents
# 2. float values inside keys were not quoted i.e stringified.
# here's the snippet, it converts the 'bad' json to 'good' ( quoted json )
@parsed = JSON.parse(Rails.root.join("app","assets","javascripts","model.js").read)
@parsed.each do |k,v|
if @parsed[k].class == Array
@parsed[k].collect! { |c| !c.is_a?(String) ? "#{c}" : c }
end
end
p @parsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment