Parse JSON from imported CSV

This commit is contained in:
Julia Friesel
2018-08-05 08:42:24 +02:00
parent ed474fd5cb
commit bce9359850
3 changed files with 71 additions and 0 deletions
+40
View File
@@ -10,6 +10,7 @@ import Share from 'react-native-share'
import getDataAsCsvDataUri from '../lib/export-to-csv'
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
import rnfs from 'react-native-fs'
import csvtojson from 'csvtojson'
import styles from '../styles/index'
import { settings as labels } from './labels'
@@ -48,8 +49,47 @@ export default class Settings extends Component {
title={labels.buttonLabel}>
</Button>
</View>
<View style={styles.homeButton}>
<Button
onPress={ importData }
title="Import data">
</Button>
</View>
</View>
</ScrollView>
)
}
}
async function importData() {
let fileInfo
try {
fileInfo = await new Promise((resolve, reject) => {
DocumentPicker.show({
filetype: [DocumentPickerUtil.allFiles()],
}, (err, res) => {
if (err) return reject(err)
resolve(res)
})
})
} catch (err) {
Alert.alert('There was a problem opening the file picker')
}
let fileContent
try {
fileContent = await rnfs.readFile(fileInfo.uri, 'utf8')
} catch (err) {
Alert.alert('Could not open file')
console.log(err)
}
let json
try {
json = await csvtojson().fromString(fileContent)
} catch(err) {
Alert.alert('Could not parse CSV file')
}
console.log(json)
}
+30
View File
@@ -2071,6 +2071,11 @@
"inherits": "2.0.1"
}
},
"bluebird": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
"integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA=="
},
"boom": {
"version": "2.10.1",
"resolved": "https://registry.npmjs.org/boom/-/boom-2.10.1.tgz",
@@ -2568,6 +2573,26 @@
"boom": "2.10.1"
}
},
"csvtojson": {
"version": "2.0.8",
"resolved": "https://registry.npmjs.org/csvtojson/-/csvtojson-2.0.8.tgz",
"integrity": "sha512-DC6YFtsJiA7t/Yz+KjzT6GXuKtU/5gRbbl7HJqvDVVir+dxdw2/1EgwfgJdnsvUT7lOnON5DvGftKuYWX1nMOQ==",
"requires": {
"bluebird": "^3.5.1",
"lodash": "^4.17.3",
"strip-bom": "^2.0.0"
},
"dependencies": {
"strip-bom": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-2.0.0.tgz",
"integrity": "sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=",
"requires": {
"is-utf8": "^0.2.0"
}
}
}
},
"dashdash": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",
@@ -4680,6 +4705,11 @@
"resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
"integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo="
},
"is-utf8": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz",
"integrity": "sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI="
},
"is-windows": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz",
+1
View File
@@ -16,6 +16,7 @@
},
"dependencies": {
"assert": "^1.4.1",
"csvtojson": "^2.0.8",
"date-range": "0.0.2",
"js-base64": "^2.4.8",
"js-joda": "^1.8.2",