2016-06-16 10 views
0

ich folgende JSON Struktur haben:Sorting JSON-Daten in Ruby

{ 
    "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" ] 
    } 
] 
} 

Ich suche die oben JSON-Datei, so gut wie es sein könnte zu sortieren. Zum Beispiel:

fields : [ 
    { 
     "name": "batch_id", 
     "type": [ "null", "string" ] 
    }, 
    ... 
    ... 
    { 
     "name": "pipeline_system", 
     "type": { 
       "type": "enum", 
       "name": "PipelineSystem", 
       "symbols": [ "backscoring", "compliance", "enterprise" ] 
       } 
    } 

Wie es sortiert die internen Arrays sowie Hashes. Ich versuche folgendes zu schreiben:

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 

aber es ist nicht sortiert die Felder Array auf der Grundlage von Namen.

Schätzen Sie jede mögliche Hilfe!

Antwort

1

Wenn ich richtig die Anforderungen verstanden:

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" 
# } 

Um alle Arrays innerhalb zu sortieren, könnte man eine rekursive Funktion vorstellen:

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

und es auf dem obersten Hash nennen.

+0

Nicht nur Namen, sondern auch Arrays innerhalb des Hash sollten sortiert werden. Wie "Symbole": ["Backscoring", "Compliance", "Unternehmen"] ' – scorix

+0

@scorix hat es, aktualisiert. – mudasobwa

+0

@scorix ich Array von Hashes bekommen konnten auch mögen: ' "type": [ "null", {SOME_HASH}, { SOME_HASH} ] ' derzeit wenn Array dann v.sort! wird nur das Array von Strings sortieren. –