# Get the name and the value of every field for an object

**URL:** https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052
**Category:** New to Julia
**Created:** [September 10, 2022, 8:30pm UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052 "2022-09-10T20:30:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)
#### Post date: [September 10, 2022, 8:30pm UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052/1 "2022-09-10T20:30:45Z")

</div>

I am writing my objects to and from my database. Database columns and field names are matching, so for convenience sake I would like to get an array of fieldnames and an array of field values to pass to the SQL `INSERT INTO` command. Is this the proper way of doing this task in Julia? I was hoping for some version of the `.function` operator. Asking to help me learn the language better.

```julia
struct Security
    ticker::String
    CUSIP::String
end

security = Security("sdf", "w23423")

julia> fieldnames(jhal.Security)
(:ticker, :CUSIP)

julia> getfield(security, :CUSIP)
"w23423"

julia> map(field -> getfield(security, field), fieldnames(jhal.Security))
("sdf", "w23423")

```

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [September 10, 2022, 10:14pm UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052/2 "2022-09-10T22:14:40Z")

</div>

First of all, your approach seems reasonable 👍 probably similar to what I would have done as well.

There is a package which I actually found just when reseaching for this question 😉  
it is [NamedTupleTools.jl](https://github.com/JeffreySarnoff/NamedTupleTools.jl#struct-construction-conversion)

With that you could do

```julia
using NamedTupleTools
nt = ntfromstruct(security)
keys(nt)
values(nt)

```

I quite like `NamedTuples`, they reduce sometimes the amount of `structs` one needs to define 😃 so it might be convenient for you too. But maybe it is also overkill here 😉

(By the way, you probably know it already but it could be that there are already packages which would help you with interfacing with SQL. In particular also DataFrames.jl in case that you haven’t seen it already.)

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [September 10, 2022, 10:47pm UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052/3 "2022-09-10T22:47:56Z")

</div>

> [@ivoytov](#):
>
> `map(field -> getfield(security, field), fieldnames(jhal.Security))`

I think you can do

```julia
getfield.(security, fieldnames(jhal.Security))

```

or

```julia
getfield.(security, fieldnames(typeof(security)))

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [September 11, 2022, 10:14am UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052/4 "2022-09-11T10:14:28Z")

</div>

I’m getting a

```julia
ERROR: MethodError: no method matching length(::Security)

```

when trying. Seems

```julia
getfield.((security,), fieldnames(Security))

```

doesn’t raise exception. Is this a version issue?  
(I’m on 1.8.0)

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [September 11, 2022, 12:00pm UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052/5 "2022-09-11T12:00:24Z")

</div>

Maybe. I didn’t actually test it. I forgot about how structs have a surprising default broadcasting behavior.

---

<div class="post-metadata">

### Author: ![alvarodiaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alvarodiaz/32/14629_2.png) [@alvarodiaz](https://discourse.julialang.org/u/alvarodiaz)
#### Post date: [November 6, 2022, 8:02pm UTC](https://discourse.julialang.org/t/get-the-name-and-the-value-of-every-field-for-an-object/87052/6 "2022-11-06T20:02:07Z")

</div>

I’ve been struggling with the same for a long time, until now that I understood what you need to pass on to the fieldnames() function.

fieldnames() does not accept the object itself as it is called, but the explicit type, then you can check all the fields of an arbitrarily given object by simply making use firs ot typeof():

for instance, to get the fields of the object that is thrown by the constructor of the package ARCHmodels  
in their tutorial ([Univariate · ARCHModels.jl](https://s-broda.github.io/ARCHModels.jl/stable/univariatetypehierarchy/)):

```julia
julia> am = UnivariateARCHModel(spec, mydata; dist=StdT(ν), meanspec=Intercept(μ))

```

you just have to do

```julia
julia> fieldnames(typeof(am))

```

without even know what is the specific way the structure is built (a bit cumbersome for this package)

```julia
UnivariateARCHModel{Float64, GARCH{1, 1, Float64}, StdT{Float64}, Intercept{Float64}}

```

and not transparent about what the names of the fields are.
