# Using records for json objects

**URL:** <https://erlangforums.com/t/using-records-for-json-objects/1053>\
**Category:** LFE Questions\
**Tags:** lfe-libraries, json\
**Created:** [February 7, 2022, 4:41pm UTC](https://erlangforums.com/t/using-records-for-json-objects/1053 "2022-02-07T16:41:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdbergmann](https://erlangforums.com/user_avatar/erlangforums.com/mdbergmann/32/1243_2.png) [@mdbergmann](https://erlangforums.com/u/mdbergmann)\
**Post date:** [February 7, 2022, 4:41pm UTC](https://erlangforums.com/t/using-records-for-json-objects/1053/1 "2022-02-07T16:41:46Z")

</div>

Hi.

I’d like to use records as json object, because the handling of records is quite nice with the available macros for records.  
For json I use the library ljson ([GitHub - lfex/ljson: An LFE library which provides a unified JSON experience](https://github.com/lfex/ljson)). However, it seems it can only deal with tuples. Now since records are tuples I thought this might actually work. But it doesn’t.

Creating a record:

```erlang
lfe> (defrecord foo (a) (b) (c))
set-foo-c
lfe> (set f (make-foo a #"a" b #"b" c #"c"))
#(foo #"a" #"b" #"c")

```

As it seems the tuples do not have the tuple structure of key:value useable for ljson for encoding because json objects require at least two entries per tuple to represent key:value.

Is there some way to convert a record into a tuple form that would work for ljson?

Manfred

---

<div class="post-metadata">

**Author:** ![rvirding](https://erlangforums.com/user_avatar/erlangforums.com/rvirding/32/178_2.png) [@rvirding](https://erlangforums.com/u/rvirding)\
**Post date:** [February 7, 2022, 9:53pm UTC](https://erlangforums.com/t/using-records-for-json-objects/1053/2 "2022-02-07T21:53:41Z")

</div>

Not directly. LFE records exactly mirror Erlang records in that they only contain the values and not the key names. You could write a special `defjson` which gives the right format with usable interfaces.

Maybe you could use records and just have double the fields, for key `foo` have `foo-key` and `foo-value` fields in the records but it would give a flat tuple `#(foo <key-name> <key-value> <key-name> <key-value> ...)` tuple. If of course this is the structure you want.

What does the json tuple look like?

---

<div class="post-metadata">

**Author:** ![mdbergmann](https://erlangforums.com/user_avatar/erlangforums.com/mdbergmann/32/1243_2.png) [@mdbergmann](https://erlangforums.com/u/mdbergmann)\
**Post date:** [February 8, 2022, 8:24am UTC](https://erlangforums.com/t/using-records-for-json-objects/1053/3 "2022-02-08T08:24:59Z")

</div>

Yep, thanks. ljson needs tuples of pairs. So the flat hierarchy wouldn’t work.  
But that’s OK. I’ll can create factory function that create the tuples I need. Once they are created they actually don’t need to change.

---

<div class="post-metadata">

**Author:** ![crownedgrouse](https://erlangforums.com/user_avatar/erlangforums.com/crownedgrouse/32/591_2.png) [@crownedgrouse](https://erlangforums.com/u/crownedgrouse)\
**Post date:** [February 8, 2022, 7:19pm UTC](https://erlangforums.com/t/using-records-for-json-objects/1053/4 "2022-02-08T19:19:49Z")

</div>

Have a look to my erlang library ?

> **[GitHub - crownedgrouse/jason: JSON encode/decode library written in Erlang](https://github.com/crownedgrouse/jason)**
>
> JSON encode/decode library written in Erlang. Contribute to crownedgrouse/jason development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![mdbergmann](https://erlangforums.com/user_avatar/erlangforums.com/mdbergmann/32/1243_2.png) [@mdbergmann](https://erlangforums.com/u/mdbergmann)\
**Post date:** [February 9, 2022, 6:53am UTC](https://erlangforums.com/t/using-records-for-json-objects/1053/5 "2022-02-09T06:53:15Z")

</div>

Thanks, I’ll check it out.  
I believe ljson works with jsx under the hood. But effectively it doesn’t matter and one probably wouldn’t need the additional abstraction.  
I’ve planed to make a facade for the json access anyhow which would allow me to easily switch json providers.

---

<div class="post-metadata">

**Author:** ![crownedgrouse](https://erlangforums.com/user_avatar/erlangforums.com/crownedgrouse/32/591_2.png) [@crownedgrouse](https://erlangforums.com/u/crownedgrouse)\
**Post date:** [February 9, 2022, 7:27am UTC](https://erlangforums.com/t/using-records-for-json-objects/1053/6 "2022-02-09T07:27:32Z")

</div>

Wiki will give you many informations and examples. Jason (nothing to do with elixir lib using same name) was created mainly for records. Enjoy.
