I have following JSON structure:
{
"name": "MessageEnvelope",
"type": "record",
"fields": [
{
"name": "message",
"type":
{
"name": "Message",
"type": "record",
"fields": [
....
]
}
},
{
"name": "pipeline_system",
"type": {
"type": "enum",
"name": "PipelineSystem",
"symbols": [ "enterprise", "backscoring", "compliance" ]
}
},
{
"name": "batch_id",
"type": [ "null", "string" ]
}
]
}
I am looking to sort the above JSON file as best as it could be. For example:
fields : [
{
"name": "batch_id",
"type": [ "null", "string" ]
},
...
...
{
"name": "pipeline_system",
"type": {
"type": "enum",
"name": "PipelineSystem",
"symbols": [ "backscoring", "compliance", "enterprise" ]
}
}
Like it sorts the internal arrays as well as hashes. I am trying to write following:
def sort(collection)
if collection.is_a?(Hash)
puts "Hash Object...."
if(collection["type"]=="record")
puts "record found... Type = #{collection["fields"].class}";
if collection["fields"].is_a?(Array)
puts "fields type is array...." #we can sort fields arrays on the basis of name
collection["fields"].sort_by{|arrayCollectionElement| arrayCollectionElement["name"] }
arrayCollection = collection["fields"] #this is array of hash...we can sort them on the basis of name..done above in sort by...
puts "class = #{arrayCollection.class}"
puts "sorted fields: #{arrayCollection}"
end
end #else it is again a hash
end
collection
end
but it is not sorting the fields array on the basis of names.
Appreciate any possible help!