I have an array that store some data like this and this is the exact input I'm using
{
"accepted" => {
"number_of_order" => {
"condition" => "between_number", "value" => "0&5"
}
}, "removed" => {
}
}
Now I'm trying to convert this array into ruby hash access like this and this is my expected output it's not what I'm getting now
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
condition: between_number
value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
But I'm unable to convert the array in ruby hash access. I also try YAML dump Yaml::dump($array); to convert into YAML then ruby hash access but nothing works.
Basically, I'm trying to do reverse of this question
How to get data from ruby hash access to laravel 8?
You can refer above link I convert this ruby hash into a laravel array but now I also wanted to convert this array into ruby hash.
My current code for converting an array to a ruby hash
$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}';
$array = json_decode($jsonEncoded, true);
dump(Yaml::dump($array));
My current code for converting ruby hash to array
$filter_data = str_replace("!ruby/hash:ActiveSupport::HashWithIndifferentAccess", "", $filter_data);
$yamlContents = Yaml::parse($filter_data);
To convert hash to yaml in ruby we can use yaml library
require 'yaml'
data = {
"accepted" => {
"number_of_order" => {
"condition" => "between_number", "value" => "0&5"
}
}, "removed" => {
}
}
puts data.to_yaml
To convert associated array to yaml in php we can use Symfony Yaml
use Symfony\Component\Yaml\Yaml;
$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}';
$array = json_decode($jsonEncoded, true);
print Yaml::dump($array);
To load php output yaml in ruby same yaml library can be used
require 'yaml'
yaml_str = ... # output generate from Symfony Yaml::dump
yaml_hash = YAML.load(yaml_str)
To load ruby output yaml in php we can use the same Symfony Yaml
$yaml_str = ...; # output generate from ruby rhash.to_yaml
$yaml_associated_array = Yaml.parse(yaml_str);
Load ruby hash to php
$data = "
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
condition: between_number
value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
";
print $data;
$data = str_replace("---", "", preg_replace("/!ruby.*Access/", "", $data));
print $data;
$result = Yaml::parse($data);
var_dump($result);
Same can be achieved in ruby
data = %Q(
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
condition: between_number
value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
)
result = str.gsub(/!ruby.*Access/, '').gsub("--- ", "")
puts result
puts YAML.load(result)
Note: We can also use other libraries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With