Column Name sorting

23 views
Skip to first unread message

Jennifer B Young

unread,
Dec 7, 2017, 1:35:45 PM12/7/17
to openr...@googlegroups.com

Hi,

I’m importing a json file into OpenRefine and I’m wondering is there some way that I can automatically sort the Column Name alphabetically? I know I can drag to rearrange but I have 50+ columns usually. The json file contains the info alphabetically before import but it isn’t retained after importing.

 

Thank  you!

Jennifer

 

Jennifer B. Young

Metadata Coordinator

Northwestern University Libraries

Northwestern University

www.library.northwestern.edu

j-yo...@northwestern.edu

847.491.8978

 

Owen Stephens

unread,
Dec 8, 2017, 11:53:39 AM12/8/17
to OpenRefine
OpenRefine doesn't have anything to do this for you automatically, although there is a bit of a kludgy workaround...

Each time you do something in OpenRefine, it is recorded in the History tab - and behind this is a set of instructions, expressed in a format called JSON, which tells OpenRefine what operation was carried out. A 'Move column' operation looks something like:

  {
    "op": "core/column-move",
    "description": "Move column Column to position 0",
    "columnName": "Column",
    "index": 0
  }

As you probably know, you can Extract operations from a project History using the Extract button on the History tab and copying the JSON of the operations you want to extract, and you can then Apply them again (either in the same or other projects) by pasting that JSON after clicking Apply on the History tab. In this instance, we can take advantage of this function to create the JSON that represents the operations we *would* carry out to get the columns in the right order. We can then use "Apply" to paste in this JSON and the operations will be applied to our project.

There are multiple options for creating the necessary JSON, but if you want to stick with OpenRefine, you can do it by:

Choose Edit Cells->Transform from the drop down at the top of any column
In the Transform dialog paste the GREL:

"["+forEachIndex(row.columnNames.sort(),i,cn,"{\"op\": \"core/column-move\",\"description\": \"Move column "+cn+" to position "+i.toString()+"\",\"columnName\": \""+cn+"\",\"index\": "+i+"}").join(",")+"]"

You don't want to apply this GREL, you just want to get the Preview to render the result.
From the Preview panel, copy the result from a single row
Cancel the Transform window
Open the History tab, click Apply, and paste in the string you've just copied from the Preview panel

Click "Perform Operations"

And that should do the trick

Screencast included here:



Owen

Owen Stephens

unread,
Dec 8, 2017, 11:55:33 AM12/8/17
to OpenRefine

Sorry- screencast attached here

Jennifer B Young

unread,
Dec 12, 2017, 9:54:23 AM12/12/17
to openr...@googlegroups.com

Thank you Owen! Worked like a charm!

-Jennifer

--
You received this message because you are subscribed to the Google Groups "OpenRefine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openrefine+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages