# Handling KeyError: key “nameOfKey” not found

**URL:** https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042
**Category:** New to Julia
**Created:** [June 25, 2020, 3:14pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042 "2020-06-25T15:14:51Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)
#### Post date: [June 25, 2020, 3:14pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/1 "2020-06-25T15:14:52Z")

</div>

I have a script that looks through a JSON structure. Every now and again, a particular key is missing. When these instances occur, I need the script to continue rather than stop. Here is my code:

```julia
for i = 1:length(marketCatalogueResult)
        marketResults = getMarketOffers(currentCatalogue[i,1])
        push!(lastTradeP1, marketResults[1]["runners"][1]["lastPriceTraded"])
        push!(lastTradeP2, marketResults[1]["runners"][2]["lastPriceTraded"])
    end
end

```

The error I sometimes get is:

```julia
KeyError: key "lastPriceTraded" not found

```

How can I amend my code to handle the stated error?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 25, 2020, 3:27pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/2 "2020-06-25T15:27:35Z")

</div>

What do you want the code to do in the case when the key doesn’t exist?

---

<div class="post-metadata">

### Author: ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)
#### Post date: [June 25, 2020, 3:45pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/3 "2020-06-25T15:45:48Z")

</div>

Insert NaN

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 25, 2020, 3:52pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/4 "2020-06-25T15:52:03Z")

</div>

You can use the `get` function to get either the value associated with a key or some default if that key does not exist. For example:

```julia
julia> results = Dict("lastPrice" => 1.0, "currentPrice" => 2.0)
Dict{String,Float64} with 2 entries:
  "currentPrice" => 2.0
  "lastPrice" => 1.0

julia> get(results, "lastPrice", NaN)
1.0

julia> get(results, "futurePrice", NaN)
NaN

```

Try running `?get` for more information.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 25, 2020, 6:09pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/5 "2020-06-25T18:09:10Z")

</div>

You may want to consider using `missing` instead, which is the Julian way to indicate missing data. `NaN` has a very specific meaning, which is that it is the result of a computation where zero was divided by zero or infinity was subtracted from itself.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 25, 2020, 6:33pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/6 "2020-06-25T18:33:33Z")

</div>

> [@StefanKarpinski](#):
>
> You may want to consider using `missing` instead, which is the Julian way to indicate missing data.

While I agree with the semantics, the temptation of just keeping things simple with a single primitive type, with no unions or risk of type instability should not be underestimated. I have some data wrangling code that deal with `Float64` columns that will never have a “true” `NaN` (things like `X_time` where `X` is a procedure that is not always called) so it is very useful to just use `NaNs` as a [sentinel value](https://en.wikipedia.org/wiki/Sentinel_value).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 25, 2020, 6:56pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/7 "2020-06-25T18:56:56Z")

</div>

Good point. NaN is also a good choice here.

---

<div class="post-metadata">

### Author: ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)
#### Post date: [June 26, 2020, 8:13am UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/8 "2020-06-26T08:13:45Z")

</div>

When I attempt to implement that solution, I get an error to do with float:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/8/08e9e547ce4541b35446251f1126af6247637946.png)

Anything obvious I am doing wrong?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 26, 2020, 8:38am UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/9 "2020-06-26T08:38:33Z")

</div>

It seems `marketResults` is an `Array{Any, 1}`, not a `Dict`?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 26, 2020, 3:34pm UTC](https://discourse.julialang.org/t/handling-keyerror-key-nameofkey-not-found/42042/10 "2020-06-26T15:34:51Z")

</div>

Looks like you need something like this:

```julia
get(marketResults[1]["runners"][1], "lastPriceTraded", NaN)

```

You can’t do the indexing and then call `get`, you replace the indexing with the call to `get`.
