SWick

Sysadmin-by-Nature

recutils JSON output
8th June 2014

Quick Python one-liner to get JSON output from recutils

data.rec

%rec: movies

Id: 4
Title: Banana Joe
Country: Italy  |  West Germany
Date: 1982
Director: Steno
Genre: Comedy
Length: 93
Add_date: 05/06/2008
Audio: German (MP3)
Identifier: 141
Location: Box 1
Media: DVD
Amount_of_Media: 1
Rating: 6
Video_file: /media/cdrom0/Banana-Joe.avi
Video_format: DX50
Viewed: 1
Borrower: none
Favourite: 0

Id: 2
Title: Baraka
Country: USA
Date: 1992
Director: Ron Fricke
Genre: Documentary
Length: 96 min
Add_date: 04/06/2008
Identifier: 858
Location: Box 1
Media: CD
Amount_of_Media: 1
Rating: 0
Video_file: /media/cdrom0/Baraka.avi
Video_format: divx
Viewed: 1
Borrower: none
Favourite: 0

Python one-liner

$ rec2csv data.rec | python -c 'import csv,json,sys; print(json.dumps(list(csv.DictReader(sys.stdin)), indent=4))'

Output

[
    {
        "Rating": "6", 
        "Identifier": "141", 
        "Title": "Banana Joe", 
        "Add_date": "05/06/2008", 
        "Country": "Italy  |  West Germany", 
        "Favourite": "0", 
        "Video_file": "/media/cdrom0/Banana-Joe.avi", 
        "Amount_of_Media": "1", 
        "Director": "Steno", 
        "Genre": "Comedy", 
        "Length": "93", 
        "Location": "Box 1", 
        "Borrower": "none", 
        "Media": "DVD", 
        "Date": "1982", 
        "Video_format": "DX50", 
        "Audio": "German (MP3)", 
        "Id": "4", 
        "Viewed": "1"
    }, 
    {
        "Rating": "0", 
        "Identifier": "858", 
        "Title": "Baraka", 
        "Add_date": "04/06/2008", 
        "Country": "USA", 
        "Favourite": "0", 
        "Video_file": "/media/cdrom0/Baraka.avi", 
        "Amount_of_Media": "1", 
        "Director": "Ron Fricke", 
        "Genre": "Documentary", 
        "Length": "96 min", 
        "Location": "Box 1", 
        "Borrower": "none", 
        "Media": "CD", 
        "Date": "1992", 
        "Video_format": "divx", 
        "Audio": "", 
        "Id": "2", 
        "Viewed": "1"
    }
]
Tags: db, nosql, python, recutils.

"People said I should accept the world. Bullshit! I don't accept the world." -- Stallman