Exporting/ importing JSON file from R

Hi! I’m trying to add metadata to my Tropy file using R. I’ve succesfully imported the JSON file to R, added my new metadata and now I need to export it so that I can create a new Tropy file with the updated database. I’ve tried using the package “jsonlite” to export the JSON file but when I try to import it to Tropy I get the following error:
“TypeError: Invalid Version: 1.12.0\n”

The problem is definitely in R because I’ve also tried importing the JSON file and exporting it without making any changes and it returns the exact same error. I exported a sample with just a few items to explore the files and see if there were major syntax differences but I only identify that R inserts brackets everywhere in the @ context section and I’m not sure if they are relevant or not, and I don’t know how to export them differently. Here is a sample of both files (I had to add random spaces after @ and links because of the forum requirements):

Tropy export:
{ “@ context”: {
“@ version”: 1.1,
“@ vocab”: “https : //tropy.org/v1/tropy#”,
“template”: {
“@ type”: “@ id”
},
“photo”: {
“@ id”: “https ://tropy.org/v1/tropy#photo”,
“@ container”: “@ list”
},
“note”: {
“@ id”: “https ://tropy.org/v1/tropy#note”,
“@ container”: “@ list”
},
“selection”: {
“@ id”: “https ://tropy.org/v1/tropy#selection”,
“@ container”: “@ list”
},
“title”: “http ://purl.org/dc/elements/1.1/title”, “date”: {
“@ id”: “http ://purl.org/dc/elements/1.1/date”,
“@ type”: “https ://tropy.org/v1/tropy#date”
},
“@ graph”: [
{
“@ type”: “Item”,
“template”: “https ://tropy.org/v1/templates/id#xaTfW7Yh”,
“title”: “file_title”,
“list”: [
“list_1”,
“list_2”
],
“tag”: [
“tag_1”,
“tag_2”
],
“photo”: [
{
“@ type”: “Photo”,
“checksum”: “5c4fc32b7efbf4bccadc305736c0dddd”,
“color”: “rgb(200,200,200,1)”,
“density”: null,
“filename”: “file_name.jpg”,
“mimetype”: “image/jpeg”,
“orientation”: 6,
“page”: 0,
“path”: “path.jpg”,
“protocol”: “file”,
“size”: 2579168,
“template”: “https ://tropy.org/v1/templates/photo”,
“angle”: 0,
“brightness”: 0,
“contrast”: 0,
“height”: 3072,
“hue”: 0,
“mirror”: false,
“negative”: false,
“saturation”: 0,
“sharpen”: 0,
“width”: 4080,
“title”: “photo_title”,
“date”: 1669632140000
}
]
}
],
“version”: “1.12.0”
}

R export:
{
“@ context”: {
“@ version”: [1.1],
“@ vocab”: [“https ://tropy.org/v1/tropy#”],
“template”: {
“@ type”: [“@ id”]
},
“photo”: {
“@ id”: [“https ://tropy.org/v1/tropy#photo”],
“@ container”: [“@ list”]
},
“note”: {
“@ id”: [“https ://tropy.org/v1/tropy#note”],
“@ container”: [“@ list”]
},
“selection”: {
“@ id”: [“https ://tropy.org/v1/tropy#selection”],
“@ container”: [“@ list”]
},
“date”: {
“@ id”: [“http ://purl.org/dc/elements/1.1/date”],
“@ type”: [“https ://tropy.org/v1/tropy#date”]
}
},
“@ graph”: [
{
“@ type”: “Item”,
“template”: “https ://tropy.org/v1/templates/id#xaTfW7Yh”,
“title”: “file_title”,
“list”: [“list_1”, “list_2”],
“tag”: [“tag_1”, “tag_2”],
“photo”: [
{
“@ type”: “Photo”,
“checksum”: “5c4fc32b7efbf4bccadc305736c0dddd”,
“color”: “rgb(200,200,200,1)”,
“filename”: “file_name.jpg”,
“mimetype”: “image/jpeg”,
“orientation”: 6,
“page”: 0,
“path”: “path.jpg”,
“protocol”: “file”,
“size”: 2579168,
“template”: “https ://tropy.org/v1/templates/photo”,
“angle”: 0,
“brightness”: 0,
“contrast”: 0,
“height”: 3072,
“hue”: 0,
“mirror”: false,
“negative”: false,
“saturation”: 0,
“sharpen”: 0,
“width”: 4080,
“title”: “photo_title”,
“date”: 1669632140000
}
]
}
],
“version”: [“1.12.0”]
}

I’m sure this is very simple to fix but I’ve not found any clear solution. If anyone has a sample script for exporting the JSON file in R so that Tropy can read it succesfully it would be amazing.

Thank you!

You can add three backticks (```) above and below your JSON context here on the forums then it will marked as preformatted text and you don’t need to make any alterations to it.

Looking at the file you posted, it looks like the conversion turned a number of string values into arrays. For example the version is ["1.12.0"] – note the extra brackets. The same transformation was applied in the @context element. If you could configure R so as to keep those values as simple strings the re-import should work.

1 Like

Thanks for the suggestions! I’ve only recently started using this software and don’t know a lot of programming so I was kind of lost.

After a few tries I manage to do it successfully, even with the database modifications. The solution was quite simple as I thought but I couldn’t figure it out without knowing what was wrong.

I will leave the R syntax here in case someone finds it useful:

library(jsonlite)
write_json(R_data, "path.json", auto_unbox=T)

“auto_unbox = TRUE” forces the function to treat vectors of length 1 as strings, whereas the default setting treats them as arrays.

This software is amazing, thanks a lot for your help!