I'm trying to parse a json file with OpenStruct. Json file has an array for Skills. When I parse it I get some extra "garbage" returned. How do I get rid of it?
json
{
"Job": "My Job 1",
"Skills": [{ "Name": "Name 1", "ClusterName": "Cluster Name 1 Skills"},{ "Name": "Name 2", "ClusterName": "Cluster Name 2 Skills"}]
}
require 'ostruct'
require 'json'
json = File.read('1.json')
job = JSON.parse(json, object_class: OpenStruct)
puts job.Skills
#<OpenStruct Name="Name 1", ClusterName="Cluster Name 1 Skills">
#<OpenStruct Name="Name 2", ClusterName="Cluster Name 2 Skills">
If by garbage, you mean
#<OpenStructand">, it is just the way Ruby represents objects when called with puts. It is useful for development and debugging, and it makes it easier to understand the difference between a String, an Array, an Hash and an OpenStruct.If you just want to display the name and cluster name, and nothing else :
It returns :
EDIT:
When you use
job = JSON.parse(json, object_class: OpenStruct), your job variable becomes an OpenStruct Ruby object, which has been created from a json file.It doesn't have anything to do with json though: it is not a json object anymore, so you cannot just write it back to a
.jsonfile and expect it to have the correct syntax.OpenStruct doesn't seem to work well with
to_json, so it might be better to removeobject_class: OpenStruct, and just work with hashes and arrays.This code reads
1.json, convert it to a Ruby object, adds a skill, modifies the job name, writes the object to2.json, and reads it again as JSON to check that everything worked fine.