Skip to content

Commit

Permalink
R script to parse json into tsv (species coordnates)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrabenmeister committed Jan 7, 2025
1 parent ceedc33 commit 3be8030
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
47 changes: 47 additions & 0 deletions scripts/GFS_IndicatorsTool/read_csv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library("rjson")


input <- fromJSON(file=file.path(outputFolder, "input.json"))

print(input)

tsv_json <-input$csv

##### Parse json text

### remove square brackets
tsv_json = substr(tsv_json, 3, nchar(tsv_json)-2)

### split lines
tsv_lines = strsplit(tsv_json, '\\), \\(')[[1]]

### convet to table
tsv_tab = do.call(rbind, strsplit(tsv_lines, ', '))

### convert to data.frame, set header
tsv_df = data.frame(tsv_tab[-1,])
colnames(tsv_df) = tsv_tab[1,]

colnames(tsv_df)[colnames(tsv_df)=="'decimal_latitude'"] = 'decimal_latitude'
colnames(tsv_df)[colnames(tsv_df)=="'decimal_longitude'"] = 'decimal_longitude'

### set coordinates as numeric
tsv_df$decimal_latitude = as.numeric(tsv_df$decimal_latitude)
tsv_df$decimal_longitude = as.numeric(tsv_df$decimal_longitude)

### set output path
tsv_out<-file.path(outputFolder, "species_obs.tsv")


### save output
write.table(tsv_df, file=tsv_out, sep='\t', quote=F, row.names = F, col.names = T)




### return output
output <- list("csv_out"=tsv_out)
print(output)

jsonData <- toJSON(output, indent=2)
write(jsonData, file.path(outputFolder,"output.json"))
23 changes: 0 additions & 23 deletions scripts/GFS_IndicatorsTool/read_csv.py

This file was deleted.

9 changes: 4 additions & 5 deletions scripts/GFS_IndicatorsTool/read_csv.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
script: read_csv.py
name: Read CSV
script: read_csv.R
name: JSON to TSV
author:
- name: Jean-Michel Lord
identifier: https://orcid.org/0009-0007-3826-1125
- name: Oliver Selmoni
license: CC0
description: "Sample python script that increments a number."
description: "Parse json string to tsv"
timeout: 1
inputs:
csv:
Expand Down

0 comments on commit 3be8030

Please sign in to comment.