0

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!

1 Answer 1

1

If I properly understood the requirements:

json = '...'

require 'json'
hash = JSON.parse json

# ⇓ let’s sort the array of fields inplace
hash['fields'].sort_by! { |o| o['name'] }

hash
#⇒ {
#  "fields" => [
#    [0] {
#      "name" => "batch_id",
#      "type" => [ "null", "string" ]
#    },
#    [1] {
#      "name" => "message",
#      "type" => {
#        "fields" => [],
#          "name" => "Message",
#          "type" => "record"
#      }
#    },
#    [2] {
#      "name" => "pipeline_system",
#      "type" => {
#           "name" => "PipelineSystem",
#        "symbols" => [ "enterprise", "backscoring", "compliance" ],
#           "type" => "enum"
#      }
#    }
#  ],
#    "name" => "MessageEnvelope",
#    "type" => "record"
# }

To sort all arrays inside, one might introduce a recursive function:

def sort_arrays hash
  hash.each do |_, v| 
    case v
    when Array then v.sort!
    when Hash then sort_arrays v
    end
  end
end

and call it on the topmost hash.

Sign up to request clarification or add additional context in comments.

3 Comments

Not only names, but also arrays inside the hash should be sorted. Such as "symbols": ["backscoring", "compliance", "enterprise" ]`
@scorix I could get array of hashes also like: "type" : [ "null" , { SOME_HASH}, {SOME_HASH} ] currently when Array then v.sort! will only sort the array of strings.
@scorix Can you please help me with link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.