How to Load CSV file in React Native - json

I want to download one CSV file in my React Native Project. I have put CSV file in assets folder in React Native Project, I'm using RNFS for fetching the same.
But when I console log RNFS.MainBundlePath it returns undefined value. Below is my Code to get the path from RNFS.
var mainBundlePath = RNFS.MainBundlePath;
var path = '/assets/Product.csv';
Any suggestions on this matter?

There you can user below library it will help me to achieve same thing in past.
Try csvtojson
you can Installation
npm i --save csvtojson
see the example given here

Related

How vscode parses json files that are used for extension configurations?

How VSCode parses json files like language-configuration.json that is used to describe language extensions? I see that these files contain comments and many, like typescript, contain trailing commans.
If such content is parsed using JSON.parse() the error will be raised.
I implement an extension that reads these config files and like to use the same parsing method that is used in vscode.
Thank you
I would assume they use the jsonc-parser - since it is written by one of the vscode team and has 3 million+ downloads a week.
npm package: jsonc-parser
I use it myself because I need to parse complicated custom settings that might have comments in them for example.
Add the package to your dependencies. npm install --save jsonc-parser
Then import it (I have it in a js extension for now):
const jsonc = require("jsonc-parser");
const rootNode = jsonc.parseTree(document.getText());

Unable to update local json file in React native app

My React Native app file structure looks like this:
/assests
/json
example.json
/components
view.js
I need to take input from the user in the view.js component and store it in the example.json file for future use. I tried using react-native-fs but could not figure out how to use it to get the absolute path of example.json and the relative path kept on throwing errors. If there is some other way to do this, it would be most appreciated.
Thanks in advance for the help!!!!
Found out what the problem was. I was trying to update files that are in the app bundle and forgot that they are read-only. So I copied the files from the app bundle to the document directory and is accessible from there. I can edit and delete from there.

Loading JSON files in React, without JSON extension

I'm trying to require some JSON files in my React app (based on CRA 3.01 with Typescript).
The normal const obj = require('./path/file.json') would work if my files had a .json extension - however, these files have .md for 'metadata' and a couple other extensions, and the standard require isn't working. The files are from a tool, so changing to .json isn't a practical option.
Doing some research, it seems the approach is to use the webpack json-loader module (the webpack json-loader docs says that working with different file extensions is the main reason for using the module). I found an example and am using this:
const context = require.context(
"json-loader!./metadata",
true,
/^\.\/.*\.md$/
);
const metadata = context("./foo.md");
I've got a minimum reproduction here (see App.tsx):
https://github.com/ericsolberg/testjson
It seems that this is correctly using the json-loader, and finding the file correctly. However, I'm getting a syntax error:
Error: Module build failed (from ./node_modules/json-loader/index.js):
SyntaxError: Unexpected token m in JSON at position 0
at JSON.parse (<anonymous>)
at Object.module.exports (/Users/***/projects/jsontest/node_modules/json-loader/index.js:4:49)
I did some research on this error, and believe the problem is that the file is being parsed twice - first by the loader configured by CreateCreactApp's default webpack config, then by the specified JSON loader.
I don't want to eject my CRA app to modify the webpack config, and would like to avoid a re-wire hack (and whatever other issues that introduces) ... does anyone know of a way to load JSON files in a CRA app, if these files don't have a JSON extension?
Here's the solution that ended up working for me.
I could eject my project, of course, and customize the webpack config to load JSON files with other extensions. It may be possible to make a rewire hack work as well.
But I realized that when I require a file that is not one of the extensions recognized by CRA's config, it instead copies that file into the build, and require('file.ext') returns the URL of the file. So I'm using axios to load the file. This means a trip to the server for something that could be done statically, but for where I'm taking this project that is actually OK (eventually it will load metadata from a server anyway).

Export from Sketch App to JSON

I want to be able to export Layer Names and properties from Sketch to JSON format. I think I can figure out how to pull the info I need from Sketch, but I haven't started to code anything, because I haven't been able to find any info about this export issue.
I'm wondering if anyone can help confirm that Sketch can only export their supported formats or if export to JSON is possible. I don't want to dive into this project only to find out that I can't end up with a JSON file.
I have been trying to work with this as well, and it turns out there are a few ways to get access to a JSON file in sketch.
use the npm package sketch2json
Turns out that if you unzip the .sketch file, there is a JSON file hiding inside.
unzip sketch-header.sketch
This creates a folder called 'pages' with the .json file inside. To get the 'Layer Names', you can just read/serialize the .json file into a string, and then the path to collect the layer names is
const obj = JSON.parse(fileString);
object.layers.forEach((layer) => {
console.log(layer.name);
});
If you rename the .sketch extension file to .zip extension file you will see as many JSON files as pages your sketch document has inside a folder called "Pages". Also some BMP previews images and other JSON related to user and document information.

Read and Write file using vs code extension

i am building an extension to parse json using vs code extension.
so my need is ,it should be able able to load .json file from a particular folder and iterate through content of the file.
Then it should allow user to select few keys from it make a new json file out of this and save it in any folder.
But i am not able to find any way to read and write files in "vs code extension".Could someone please help me.
If you want to read the current edit state of a file you can use the following API workspace function:
vscode.workspace.openTextDocument(uri).then((document) => {
let text = document.getText();
});
This will show you the current state of the file including unpersisted changes. document is of type TextDocument and has isDirty set to true if it has pending changes.
Since the extension runs in nodejs, you should be able to use any nodejs module built-in or installed by npm in the usual way.
For your purpose you will be OK with the built-in fs module: https://nodejs.org/dist/latest-v6.x/docs/api/fs.html
In your extension you will need to import the required module, so your code file should contain this:
let fs = require("fs");
and then use the methods in the usual way, eg. fs.fileReadSync( filename, encoding ) ...
Please not that there is one exception. If you install a nodejs module containing compiled, binary code, it will not run in the extension and instead you will see an error message saying something like %1 is not a valid Win32 application. Pure javascript modules are OK, though.
VSCode extensions are running in node.js. Therefore you can use any available node.js package/module within your extension. For instance, check out this question for reading JSON.
For JSON, you just need to require or import the JSON file, such as:
const jsonObject = require('./myJSONfile.json');
// do something
For JSON with comments, you can use node-jsonc-parser.
After the manipulation, you could use the fs module of nodej.js to write to the disk.