-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_to_csv_new.py
More file actions
290 lines (243 loc) · 9.19 KB
/
Copy pathjson_to_csv_new.py
File metadata and controls
290 lines (243 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
Convert JSON files to CSV format
Generates CSV with DOI, dataset name, and file metadata columns
Supports both Dataverse JSON structures:
- Modern "search/export" format: top-level keys + latestVersion
- Legacy "datasetVersion" format from older API endpoints
"""
import os
import sys
import argparse
from typing import List, Dict, Any, Optional
from utils import (
load_json_file,
save_csv_file,
get_json_files_from_directory
)
def _get_version_block(json_data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
Return whichever version block is present in the JSON.
Dataverse exports use one of two keys depending on the API endpoint:
- /api/datasets/export → "datasetVersion"
- /api/search or newer → "latestVersion"
We check both so the rest of the code only has to deal with one object.
"""
for key in ("latestVersion", "datasetVersion"):
block = json_data.get(key)
if isinstance(block, dict) and block:
return block
return None
def extract_doi(json_data: Dict[str, Any]) -> str:
"""
Extract a DOI from common Dataverse JSON field names.
Priority order:
1. Top-level datasetPersistentId
2. datasetPersistentId inside the version block (latestVersion / datasetVersion)
3. Top-level persistentUrl ← this is where your sample file stores it
4. Top-level identifier
"""
if json_data.get("datasetPersistentId"):
return json_data["datasetPersistentId"]
version_block = _get_version_block(json_data)
if version_block and version_block.get("datasetPersistentId"):
return version_block["datasetPersistentId"]
if json_data.get("persistentUrl"):
return json_data["persistentUrl"]
if json_data.get("identifier"):
return json_data["identifier"]
return ""
def extract_title_from_citation(json_data: Dict[str, Any]) -> str:
"""
Extract the dataset title from nested Dataverse citation metadata.
Looks in (in order):
1. Top-level datasetName / title shortcuts
2. metadataBlocks.citation.fields inside latestVersion or datasetVersion
"""
if json_data.get("datasetName"):
return json_data["datasetName"]
if json_data.get("title"):
return json_data["title"]
version_block = _get_version_block(json_data)
if not version_block:
return "Unknown Dataset"
fields = (
version_block
.get("metadataBlocks", {})
.get("citation", {})
.get("fields") or []
)
for field in fields:
if field.get("typeName") == "title":
value = field.get("value")
if isinstance(value, str):
return value
if isinstance(value, dict):
return value.get("value", "")
if isinstance(value, list) and value:
first = value[0]
return first.get("value", "") if isinstance(first, dict) else str(first)
return "Unknown Dataset"
def extract_files(json_data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Return the list of file entries from whichever structure is present.
Checks (in order):
1. latestVersion.files ← your sample file
2. datasetVersion.files ← older export format
3. Top-level data[] ← some legacy formats
4. Top-level files[]
"""
version_block = _get_version_block(json_data)
if version_block:
files = version_block.get("files")
if isinstance(files, list):
return files
if isinstance(json_data.get("data"), list):
return json_data["data"]
if isinstance(json_data.get("files"), list):
return json_data["files"]
return []
def get_file_info(file_entry: Dict[str, Any]) -> Dict[str, Any]:
"""
Normalise a single file entry from the Dataverse JSON.
Each entry has two layers:
- Outer (file_entry): label, description, directoryLabel, restricted, categories
- Inner (dataFile): id, filename, filesize, contentType, md5, …
Description lives on the outer layer and is available even for
restricted files — no API key required to read it from the JSON.
"""
data_file = file_entry.get("dataFile")
if not isinstance(data_file, dict):
data_file = file_entry # fallback for flat structures
# Prefer outer-layer directoryLabel; fall back to inner one
directory_label = (
file_entry.get("directoryLabel")
or data_file.get("directoryLabel", "")
)
return {
"id": data_file.get("id", ""),
"label": file_entry.get("label") or data_file.get("filename", ""),
"description": file_entry.get("description", ""), # outer layer — always present
"directoryLabel": directory_label,
"filesize": data_file.get("filesize", ""),
"contentType": data_file.get("contentType", data_file.get("dataType", "")),
"restricted": file_entry.get("restricted", False),
"md5": data_file.get("md5", ""),
"categories": ", ".join(file_entry.get("categories") or []),
}
def json_to_csv_converter(
json_files: List[str],
output_csv: str,
dataset_names: Dict[str, str] = None,
) -> None:
"""
Convert multiple Dataverse JSON metadata files into a single CSV.
Args:
json_files: List of paths to JSON files.
output_csv: Output CSV file path.
dataset_names: Optional {DOI: name} override map.
"""
csv_rows = []
dataset_names = dataset_names or {}
for json_file in json_files:
try:
print(f"Processing: {json_file}")
json_data = load_json_file(json_file)
doi = extract_doi(json_data)
if not doi:
print(f" Warning: No DOI found in {json_file}")
continue
dataset_name = dataset_names.get(doi) or extract_title_from_citation(json_data)
files = extract_files(json_data)
if not files:
print(f" Warning: No files found in {json_file}")
csv_rows.append({
"DOI": doi,
"dataset_name": dataset_name,
"file_id": "",
"file_label": "",
"file_description": "",
"file_path": "",
"file_size": "",
"content_type": "",
"restricted": "",
"md5": "",
"categories": "",
"original_description": "",
"new_description": "",
"new_file_path": "",
"status": "no_files",
})
continue
for file_entry in files:
info = get_file_info(file_entry)
csv_rows.append({
"DOI": doi,
"dataset_name": dataset_name,
"file_id": info["id"],
"file_label": info["label"],
"file_description": info["description"],
"file_path": info["directoryLabel"],
"file_size": str(info["filesize"]),
"content_type": info["contentType"],
"restricted": str(info["restricted"]),
"md5": info["md5"],
"categories": info["categories"],
"original_description": info["description"],
"new_description": "",
"new_file_path": "",
"status": "pending",
})
except Exception as e:
print(f" Error processing {json_file}: {e}")
continue
if csv_rows:
fieldnames = [
"DOI",
"dataset_name",
"file_id",
"file_label",
"file_description",
"file_path",
"file_size",
"content_type",
"restricted",
"md5",
"categories",
"original_description",
"new_description",
"new_file_path",
"status",
]
save_csv_file(csv_rows, output_csv, fieldnames)
print(f"\nSuccessfully converted {len(csv_rows)} rows to {output_csv}")
else:
print("No data to convert")
def main():
parser = argparse.ArgumentParser(
description="Convert Dataverse JSON metadata files to CSV format"
)
parser.add_argument(
"--input-dir",
type=str,
default="./data/json_templates",
help="Directory containing JSON files (default: ./data/json_templates)",
)
parser.add_argument(
"--output-csv",
type=str,
default="./data/metadata.csv",
help="Output CSV file path (default: ./data/metadata.csv)",
)
parser.add_argument(
"--json-file",
type=str,
help="Single JSON file to convert (alternative to --input-dir)",
)
args = parser.parse_args()
json_files = [args.json_file] if args.json_file else get_json_files_from_directory(args.input_dir)
if not json_files:
print(f"No JSON files found in {args.input_dir}")
sys.exit(1)
json_to_csv_converter(json_files, args.output_csv)
if __name__ == "__main__":
main()