I made a recursive function to extract a value from a JSON given the key for that value:
function extract(json, key)
req = []
for (k, v) in pairs(json)
if k == key
println("FOUND $key == $k")
println("value: $v")
println("type: $(typeof(v))")
push!(req, v)
elseif typeof(v) <: AbstractDict
extract(v, key)
end
end
req
end
I tested this function with the following Dict
: Dict(:a => Dict(:b => "wrong"), :Src => "GOLD")
With the expected result: It returns an Array{Any, 1}
with the value "GOLD"
However, when I try it with the real use case in mind it doesn’t return anything. The real use case is that I get two JSON payloads from an HTTP request and read them using JSON3.read()
. I then put those two JSONs in a dict. The resulting type of the Dict is: Dict{Symbol,JSON3.Object{Array{UInt8,1},Array{UInt64,1}}}
.
The print statements run when it finds the key but for some reason it doesn’t push the value (v
) to the req
List and so it returns nothing. The same thing happens if I substitute the push!()
with a return v
instead. Nothing is returned.
Any ideas?
Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message ) (More Info)