i have a function that retrieves me a JSON, now i would like to add an image to each entry in base64 (depending on the header which image i will add) and it has multiples levels. How can this be done?
This is a JSON example of what my functions already retrieves:
"Animals": {
"Cat": true,
"Dog": true,
"Dino": {},
"Shark": {
"White": true,
"Regular": true
},
}
And I would like to remove the true
and instead of it, replace it with an image (base64 format) that would depend on the tag.
the finished JSON would look something like:
"Animals": {
"Cat": YWJjMTIzIT8kKiYoKSctPUB+abc123!?$*&()'-=@~,
"Dog": QWxhZGRpbjpvcGVuIHNlc2FtZQ==,
....
Ok, here is an answer which addresses your question as it stands. I suspect that it's not very helpful. It's still not clear what you're actually having difficulty with.
Let's say i have a valid JSON string (Note: yours isn't valid: i had to remove a comma and wrap the whole thing in curly braces to make it valid)
mystring = "{\"Animals\": { \"Cat\": true, \"Dog\": true, \"Dino\": {}, \"Shark\": { \"White\": true, \"Regular\": true } }}"
I convert it to a ruby hash with JSON.parse:
myhash = JSON.parse(string)
=> {"Animals"=>{"Cat"=>true, "Shark"=>{"Regular"=>true, "White"=>true}, "Dog"=>true, "Dino"=>{}}
I change the value of Cat to the desired value. Note that i have no idea whatsoever if this value is correct, or how it was generated, it's just copied from your question.
myhash["Animals"]["Cat"] = "YWJjMTIzIT8kKiYoKSctPUB+abc123!?$*&()'-=@~"
=> "YWJjMTIzIT8kKiYoKSctPUB+abc123!?$*&()'-=@~"
I convert it back to JSON.
myhash.to_json
=> "{\"Animals\":{\"Cat\":\"YWJjMTIzIT8kKiYoKSctPUB+abc123!?$*&()'-=@~\",\"Shark\":{\"Regular\":true,\"White\":true},\"Dog\":true,\"Dino\":{}}}"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments