Extracting values from a specific key in a json

Viewed 55

I have this JSON:

  {"eletrics":{"USA":['trcd', 'moty', 'pily'], "EUROPE" :['bny','ury','try']}, "fuel":{"USA":['tre', 'motr', 'pilow'], "EUROPE" :['bty','tryn','tre']}}

How can I extract all strings in the key==EUROPE

The outuput should be:

 europe = ['bny','ury','try','bty','tryn','tre']

in python I would do:

df = df.loc["EUROPE"].explode()

How can I do this in Julia:

1 Answers

Here's one way to do it:


julia> jstr = """
  {"eletrics":{"USA":["trcd", "moty", "pily"], "EUROPE" :["bny","ury","try"]}, "fuel":{"USA":["tre", "motr", "pilow"], "EUROPE" :["bty","tryn","tre"]}}
       """;

julia> jobj = JSON3.read(jstr)
JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 2 entries:
  :eletrics => {…
  :fuel     => {…

julia> europe = reduce(vcat,
                       (jobj[key][:EUROPE] for key in keys(jobj)));


julia> println(europe)
["bny", "ury", "try", "bty", "tryn", "tre"]

Related