lib/nice/hash/add_to_ruby.rb
###########################################################################
#  In case the string is a json it will return the keys specified. the keys need to be provided as symbols.
#  In case the string is not a json then it will notify the error and return empty Hash
#  input:
#    keys:
#       1 value with key or an array of keys
#         In case the key supplied doesn't exist in the hash then it will be returned nil for that one
#  output:
#    if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash of arrays.
#    if no keys given: the json string as a ruby structure.
#    if no json string or wrong json string, an empty hash.
#    if one key supplied and doesn't exist on the json string then an empty hash
###########################################################################
In case the string is a json it will return the keys specified. the keys need to be provided as symbols.
In case the string is not a json then it will notify the error and return empty Hash
input:
keys:
1 value with key or an array of keys
In case the key supplied doesn't exist in the hash then it will be returned nil for that one
output:
if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash of arrays.
if no keys given: the json string as a ruby structure.
if no json string or wrong json string, an empty hash.
if one key supplied and doesn't exist on the json string then an empty hash
In case the string is a json it will return the keys specified . the keys need to be provided as symbols . In case the string is not a json then it will notify the error and return empty Hash input : keys : 1 value with key or an array of keys In case the key supplied doesn ' t exist in the hash then it will be returned nil for that one output : if keys given : a hash of ( keys values ) or the value if the key is found more than once in the json string then it will be return a hash of arrays . if no keys given : the json string as a ruby structure . if no json string or wrong json string an empty hash . if one key supplied and doesn ' t exist on the json string then an empty hash
def json(*keys)
    require "json"
    result = {}
    begin
      feed_symbols = JSON.parse(self, symbolize_names: true)
      if !keys.empty?
        result_tmp = if keys[0].is_a?(Symbol)
                       NiceHash.get_values(feed_symbols, keys)
                     else
                       {}
                     end
        if result_tmp.size == 1
          result = if result_tmp.values.is_a?(Array) && (result_tmp.values.size == 1)
                     if result_tmp.values[0].nil?
                       {}
                     else
                       result_tmp.values[0]
                     end
                   else
                     result_tmp.values
                   end
        else
          result_tmp.each do |key, value|
            result[key] = if (value.is_a?(Array) || value.is_a?(Hash)) && (value.size == 1)
                            value[0]
                          else
                            value
                          end
          end
        end
      else
        result = feed_symbols
      end
    rescue StandardError => stack
      puts "#{stack.backtrace[-1]} #{stack.to_s}"
      puts stack.backtrace.join("\n\t") if $DEBUG
    end
    result
  end
def json ( * keys ) require "json" result = { } begin feed_symbols = JSON . parse ( self , symbolize_names : true ) if ! keys . empty? result_tmp = if keys [ 0 ] . is_a? ( Symbol ) NiceHash . get_values ( feed_symbols , keys ) else { } end if result_tmp . size == 1 result = if result_tmp . values . is_a? ( Array ) && ( result_tmp . values . size == 1 ) if result_tmp . values [ 0 ] . nil? { } else result_tmp . values [ 0 ] end else result_tmp . values end else result_tmp . each do | key , value | result [ key ] = if ( value . is_a? ( Array ) || value . is_a? ( Hash ) ) && ( value . size == 1 ) value [ 0 ] else value end end end else result = feed_symbols end rescue StandardError => stack puts "#{stack.backtrace[-1]} #{stack.to_s}" puts stack . backtrace . join ( "\n\t" ) if $DEBUG end result end
In case the string is a json it will return the keys specified.
In case the string is a json it will return the keys specified .
keys