# How to replace 'Nothing' data type in an array

**URL:** <https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366>\
**Category:** New to Julia\
**Created:** [July 22, 2024, 10:59pm UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366 "2024-07-22T22:59:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 22, 2024, 10:59pm UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/1 "2024-07-22T22:59:54Z")

</div>

Hi everyone,

I’m currently trying to untangle a pretty messy multi-nested json file by flattening them out manually and curating them into a dataframe (mostly following the method described here: [https://julia.school/julia/json/](https://julia.school/julia/json/)).

Some of the sub-nested elements are giving me some issues - I ended up with arrays like below, let’s say named ‘nest1\_array’:

```julia
 Dict{String, Any}("sub" => "K2")
 Dict{String, Any}("sub" => "A5")
 Dict{String, Any}("sub" => "G1")
 Dict{String, Any}("sub" => "G1")
 Dict{String, Any}("sub" => "B1")
 nothing
 Dict{String, Any}("sub" => "GD2")
 nothing
 nothing
 nothing
 Dict{String, Any}("sub" => "A6")
 Dict{String, Any}("sub" => "C1")

```

Now my goal is do below to isolate ‘sub’ elements from above into its own array and push it into a manually curated dataframe, via:

```julia
clean_array=Any[]

for i in eachindex(nest1_array)
       push!(clean_array, nest1_array[i]["sub"])
end

curated_df[!, :sub]=clean_array

```

Unfortunately, trying to isolate ‘sub’ entries from the nest1\_array does not work, giving me below error message:

```julia
ERROR: MethodError: no method matching getindex(::Nothing, ::String)

```

Now, I can vaguely understand the error is because ‘nothing’ is its own data type, and indexing it along with the rest of the strings might be problematic (please do feel free to point out if this is wrong, or if there are better elaborations for the beginner!).

But what would be a solution to this issue? Is there a preexisting method or function that can be used to convert ‘::Nothing’ data rows into a text, like ‘N/A’?

So far only solution I found was to export the array to a text file and just use shell utilities + regex to clean the output, but I would far, far prefer a pure Julia solution. Especially since I have dozens more of these nested columns to go through…

As usual, any advice would be appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [July 22, 2024, 11:29pm UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/2 "2024-07-22T23:29:40Z")

</div>

The most straightforward way is to use an if statement:

```julia
for i in eachindex(nest1_array)
       if isnothing(nest1_array[i])
           push!(clean_array, "N/A")
       else
           push!(clean_array, nest1_array[i]["sub"])
       end
end

```

You can also use `replace`:

```julia
nest1_array_replaced = replace(nest1_array, nothing => Dict("sub" => "N/A")
curated_df[!, :sub] = [x["sub"] for x in nest1_array_replaced]

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [July 23, 2024, 2:12am UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/3 "2024-07-23T02:12:47Z")

</div>

The `if` solution by @Satvik is closest to your original code, but a oneliner alternative to the `replace` solution could be:

```julia
curated_df[!, :sub] = [isnothing(x) ? "N/A" : x["sub"] for x in nest1_array]

```

---

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 23, 2024, 2:48am UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/4 "2024-07-23T02:48:16Z")

</div>

Hi,

Thank you for responding.

I’m a bit befuddled right now - the control flow + ‘isnothing’ based method you described sometimes works and sometimes doesn’t.

For example, here’s a version of your script I executed on a JSON-to-dictionary array (generated by JSON.jl).

```julia
for i in eachindex(results_col)
        if isnothing(results_col[i])
                push!(enriched_sample_array, "N/A")
        else
                push!(enriched_sample_array, results_col[i]["enriched_sample"])
        end
end

```

One would assume ‘nothing’ entries (not present in original data, but present in the JSON.jl generated array) should be replaced with “N/A” - but it’s not the case. Here’s the output of the array after processing via above method:

```julia
      nothing
      nothing
      nothing
      nothing
      nothing
      nothing
 false
  true
  true
 false
 false
  true
     ⋮
  true
  true
  true
  true
  true
  true
      nothing
  true
 false
 false
      nothing

```

And so and so forth for a few of thousand rows.

Which is a problem, since the ‘nothing’ on the column is probably a type of data, not just string, as this error message suggests when I try to export the final dataframe as an external file.

```julia
ERROR: LoadError: A `nothing` value was found in column 9 and it is not a printable value.
There are several ways to handle this situation:
1) fix the data, perhaps replace `nothing` with `missing`,
2) use `transform` option with a function to replace `nothing` with whatever value (including `missing`), like `CSV.write(...; transform=(col, val) -> something(val, missing))` or
3) use `TableOperations.transform` option to transform specific columns

```

Granted, I was able to get around above error by using the transform option as suggested by the error message (like this:)

```julia
CSV.write("dataframe.tsv", df; delim='\t', transform=(col,val)->something(val,"N/A"))

```

But I’m a little bothered by the fact that there’s still ‘Nothing’ data on some of the columns even after initial loop, and even after the CSV.write with transform I see blank spaces on some of the columns rather than N/A.

So - I feel like there’s a weird predictability issue I’m fighting with. I’m sure I’ve done something wrong, but Julia script used to do what I’ve done are so simple I’m not sure what it could be.

Any ideas would be appreciated!

---

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 23, 2024, 2:50am UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/5 "2024-07-23T02:50:07Z")

</div>

This looks great!

I think I can generally understand what’s going on, but would you mind adding a light explanation on what each of the symbols and commands do, just to make sure?

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [July 23, 2024, 3:35am UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/6 "2024-07-23T03:35:31Z")

</div>

Looking at

```julia
curated_df[!, :sub] = [isnothing(x) ? "N/A" : x["sub"] for x in nest1_array]

```

The left hand side of the `=` is the same as what you have already used, so hopefully you understand it already, it is just a way of assigning values to a full column of the `Dataframe`.

The right hand side is what is called an “array comprehension”, it is a way of applying a function to each member of an array, so this would be equivalent to

```julia
function f(x)
    return isnothing(x) ? "N/A" : x["sub"]
end

curated_df[!, :sub] = [f(x) for x in nest1_array]

```

The `?` `:` combination is an operator known from C and many other languages, it is just a shorthand for:

```julia
function f(x)
    if isnothing(x)
        return "N/A"
    else
        return x["sub"]
    end
end

curated_df[!, :sub] = [f(x) for x in nest1_array]

```

Unlike some other languages however, Julia requires whitespace around both the `?` and the `:`

---

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 23, 2024, 5:50am UTC](https://discourse.julialang.org/t/how-to-replace-nothing-data-type-in-an-array/117366/7 "2024-07-23T05:50:35Z")

</div>

Ah, I understand now. Thank you for the very thorough explanation!
