# Preserve record mode hierarchy for export / templating

**URL:** <https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977>\
**Category:** Support and Helpdesk\
**Created:** [December 24, 2024, 3:37pm UTC](https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977 "2024-12-24T15:37:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![archilecteur](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/archilecteur/32/1099_2.png) [@archilecteur](https://forum.openrefine.org/u/archilecteur)\
**Post date:** [December 24, 2024, 3:37pm UTC](https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977/1 "2024-12-24T15:37:44Z")

</div>

One of OpenRefine's most useful basic features is the 'record' mode, which allows you to replicate nested structures. However, when I try to export to JSON format using the 'templating' output menu, I lose this hierarchy. I can invoke it to populate a field (row.record.cells), but each object in the output file is flat, one object per row:

{  
"rows : [  
{  
"key\_1” : "value of column 1 of row 1",  
"key\_2” : "value of column 2 of row 1",  
"key\_3” : "value of column 3 in row 1",  
"key\_4": "value of column 4 in row 1"  
]  
},  
{  
"rows” : [  
{  
"key\_1” : "value of column 1 of row 2 (which is empty)",  
"key\_2” : "value of column 2 in row 2 (which is empty)",  
"key\_3": "value of column 3 in line 2",  
"key\_4": "value of column 4 in line 2"  
]  
}

Whereas the structure I need would look like this:

{  
"records” : [  
{  
"key\_1” : "value of column 1 of row 1",  
"key\_2” : "value of column 2 of row 1",  
"parentKey" : [  
{  
"key\_3” : "value of column 3 in row 1",  
"key\_4” : "value of column 4 in line 1"  
},  
{  
"key\_3” : "value of column 3 in row 2",  
"key\_4": "value of column 4 in row 2"  
}  
]  
]  
}

As I said, I can invoke the 'record' index to populate the missing values in the second line with the values in the first line, but I still get two objects.

Can it be done?

---

<div class="post-metadata">

**Author:** ![thadguidry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/thadguidry/32/653_2.png) [@thadguidry](https://forum.openrefine.org/u/thadguidry)\
**Post date:** [December 25, 2024, 1:23am UTC](https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977/2 "2024-12-25T01:23:21Z")

</div>

It's probably not evident, but you'll need to jsonize (sometimes/sometimes not) the output from the overlay model of `row.record.cells` output depending on the hierarchy (formatting, arrays, etc.) you are wanting.

Simple example where my first column "A" is the record identifier/key:

```auto
    {
      "recordkey": {{row.record.cells["A"].value}}
      "B" : {{jsonize(row.record.cells["B"].value)}}
      "C" : {{jsonize(row.record.cells["C"].value)}}
    }

```

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/openrefine/original/2X/6/66c0d135aeaf7129c9a9f06da83719b91f784c25.png)

---

<div class="post-metadata">

**Author:** ![thadguidry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/thadguidry/32/653_2.png) [@thadguidry](https://forum.openrefine.org/u/thadguidry)\
**Post date:** [December 25, 2024, 1:29am UTC](https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977/3 "2024-12-25T01:29:14Z")

</div>

But my hunch is that you might benefit from a JSON records exporter, so you might want to 👍 on this issue:

> <https://github.com/OpenRefine/OpenRefine/issues/1897>
>
> \*\*Is your feature request related to a problem or area of OpenRefine? Please des…cribe.\*\*
> JSON data source(s): Recreate original json(s) with changed values
> 
> \*\*Describe the solution you'd like\*\*
> It would be great if after importing and cleaning we can recreate the original json file(s) but with those values changed.
> 
> \*\*Describe alternatives you've considered\*\*
> I have to export as CSV, and write a program to load the original json file, delete the relevant node and assign to it values read from the csv.
> 
> \*\*Additional context\*\*
> I imported data from an array inside each one of multiple jsons that have the same structure. OpenRefine did a great job of extracting just those values and bringing them all in one column for working on. After data-cleaning job is done, I now want to recreate the same json files that I had loaded, but with the values internally replaced with the changed values. At present the original structures are lost and I can only export the cleaned data as a single csv, excel etc.
> 
> This can have tremendous application in cases where our data is in json or similar non-tabular format, we have to clean it, but we still have to work with the data in that json format itself; we cannot lose that structure in the process of data cleaning.
> 
> \*\*Extra: What it would take to do this:\*\*
> \- Store the complete json(s) loaded in the OpenRefine project, including all the nodes, data that weren't selected.
> \- Save the internal path of the data item that was extracted in a column just like presently a separate column is made to tell which file the data came from. Example: In my case it would be: data.stopsArray0\[0\], data.stopsArray0\[1\], etc.
> \- Make an extension or export option of "recreate original json(s) with changed data" and do that!

---

<div class="post-metadata">

**Author:** ![thadguidry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/thadguidry/32/653_2.png) [@thadguidry](https://forum.openrefine.org/u/thadguidry)\
**Post date:** [December 25, 2024, 1:36am UTC](https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977/4 "2024-12-25T01:36:12Z")

</div>

Actually, I always forget about the most obvious workaround...  
Jsonize and build the JSON you want inside a new OpenRefine column in the datagrid, then export and select only that column? But more pain/less pain, dunno?

---

<div class="post-metadata">

**Author:** ![archilecteur](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/archilecteur/32/1099_2.png) [@archilecteur](https://forum.openrefine.org/u/archilecteur)\
**Post date:** [December 30, 2024, 5:19pm UTC](https://forum.openrefine.org/t/preserve-record-mode-hierarchy-for-export-templating/1977/5 "2024-12-30T17:19:46Z")

</div>

Hey @Thadguidry,  
Sorry for the delay in responding!  
Thanks again for the prompt responses and helpful suggestions.  
In between bites of turkey, I finally explored your suggestions for tackling this challenging task. Selective use of jsonize does indeed allow me to mimic nesting a bit more, but I get several rows with identical content, not true nesting. Is this something I'm doing wrong?  
As suggested, I've added a thumbs-up to @answerquest's “feature request” (from dec. 2018!) Indeed, a JSON exporter would be ideal.  
I considered building the JSON object into the main interface, — that's what I've done in the past. In this case, with three dozen columns, and a three-level hierarchy, multiple properties to a single nested object, I admit I'm afraid I'll get tangled up and make mistakes.  
In the end, I resorted to a Python script (in a notebook) to get my objects nested correctly, relying on a unique identifier produced after some careful ordering in OpenRefine.  
A solution within OpenRefine is always preferable, of course.
