Convert csv file to json object and save to file? - json

I am using https://github.com/Keyang/node-csvtojson to convert a csv file into json object but not sure how to create a js file from the output. Using the command
$ csvtojson ./mycsv.csv
I want to save the output into new file, how can I do that using command line?

$ csvtojson ./mycsv.csv > converted.json
See the docs.

From the help section, it doesn't seem like csvtojson cli supports it. But you can always use > to output the command output to a file.
Example: ls > dir_structure.txt.

Related

How to read a variable in json file when accessing that json file from a batch file

I have batch test.bat where I have set variables and I want access those variables in json file.
json file
{
"jdbcUrl" : "jdbc:postgresql://&{openidm.repo.host}:&{openidm.repo.port}/&
{test.rds.dbname}"
}
batch file looks like
set hostname="abc.host"
-Dopenidm.repo.host=%hostname%
&{openidm.repo.host} is working fine in shell script can you pls suggest instead of & what I need to use for my bat file
for bat file is is somethinh like this %{openidm.repo.host}?????

How to export JMeter results to JSON?

We run load tests with JMeter and would like to export result data (throughput, latency, requests per second etc.) to JSON, either a file or STDOUT. How can we do that?
JMeter can save the results in a CSV format with header.
(Do not forget to select Save Field Names - it is OFF by default)
Then you can use this tool to covert the CSV to a JSON.
http://www.convertcsv.com/csv-to-json.htm
EDIT
JMeter stores the result in XML or CSV format. XML is by default (with .jtl extension). But It is always recommended to save the result in csv format.
If you want to convert XML to JSON
http://www.utilities-online.info/xmltojson/#.U9O2ifldVBk
If you are planning to use CSV, To save the result in CSV format automatically
When you are running your test via command line, to save the result in csv for a specific test
%JMETER_HOME%\bin\jmeter.bat" -n -t %TESTNAME% -p %PROPERTY_FILE_PATH% -l %RESULT_FILE_PATH% -j %LOG_FILE_PATH% -Djmeter.save.saveservice.output_format=csv
Or
You can update the jmeter.properties in bin folder to enable below property (for any test you run)
jmeter.save.saveservice.output_format=csv
Hope, it is clear!
There is no OOTB solution for this but you could inspire yourself from this patch:
https://issues.apache.org/bugzilla/show_bug.cgi?id=53668

Getting Error on Updating CSV file into SOLR: Expected fieldnames in CSV input

I wanna Import a csv File using File Path But whene i want to upload this i get msg= Expected fieldnames in CSV input ErrorNumber=400
I am using this url :
http://localhost:8983/solr/Log/update?wt=json&commitWithin=1000&overwrite=true&stream.file=Log\Log.csv&stream.contentType=text/plain;charset=utf-8
Im using SOLR 4.2 and my file is Store in c:. i am using Windows 8.1
thank you
In my experience this problem often happens when the path of CSV file is invalid.
For example:
SOLR_URL="localhost:8983/solr/collection_1"
DATA_FILE=/tmp/file_data.csv
curl "$SOLR_URL/update?commit=true" --data-binary #$DATA_FILE -H 'Content-type:application/csv'
I had to double check the DATA_FILE path.
I found a solution. I changed the query URL to something like this and it worked
http://localhost:xxxx/solr/update/csv?stream.file=<remote path>/Log.csv&stream.contentType=text/plain;charset=utf-8
just replace "remote path" with your own path

Best way to format large JSON file? (~30 mb)

I need to format a large JSON file for readability, but every resource I've found (mostly online) doesn't deal with data say, above 1-2 MB. I need to format about 30 MB. Is there any way to do this, or any way to code something to do this?
With python >= 2.6 you can do the following:
For Mac/Linux users:
cat ugly.json | python -mjson.tool > pretty.json
For Windows users (thanks to the comment from dnk.nitro):
type ugly.json | python -mjson.tool > pretty.json
jq can format or beautify a ~100MB JSON file in a few seconds:
jq '.' myLargeUnformattedFile.json > myLargeBeautifiedFile.json
The command above will beautify a single-line ~120MB file in ~10 seconds, and jq gives you a lot of json manipulation capabilities beyond simple formatting, see their tutorials.
jsonpps is the only one worked for me (https://github.com/bazaarvoice/jsonpps).
It doesn't load everything to RAM unlike jq, jsonpp and others that I tried.
Some useful tips regarding installation and usage:
Download url: https://repo1.maven.org/maven2/com/bazaarvoice/jsonpps/jsonpps/1.1/jsonpps-1.1.jar
Shortcut (for Windows):
Create file jsonpps.cmd in the same directory with the following content:
#echo off
java -Xms64m -Xmx64m -jar %~dp0\jsonpps-1.1.jar %*
Shortcut usage examples:
Format stdin to stdout:
echo { "x": 1 } | jsonpps
Format stdin to file
echo { "x": 1 } | jsonpps -o output.json
Format file to file:
jsonpps input.json -o output.json
Background-- I was trying to format a huge json file ~89mb on VS Code using the command (Alt+Shift+F) but the usuals, it crashed. I used jq to format my file and store it in another file.
A windows 11 use case is shown below.
step 1- download jq from the official site for your respective OS - https://stedolan.github.io/jq/
step 2- create a folder in the C drive named jq and paste the executable file that you downloaded into the folder. Rename the file as jq (Error1: beware the file is by default an exe file so do not save it as 'jq.exe' save it only as 'jq')
step 3- set your path variable to the URL of the executable file.
step 4- open your directory on cmd where the json file is stored and type the following command - jq . currentfilename.json > targetfilename.json
replace currentfilename with the file name that you want to format
replace targetfilename with the final file name that you want your data formatted in
within seconds you should see your target file in the same directory in a formatted version which can now be opened on VS Code or any editor for that matter. Any error related to the recognizability of jq as a command can be traced back with high probability to Error 1.
jq jquery json data-preprocessing data-cleaning
You can use Notepad++ (https://notepad-plus-plus.org/downloads/) for formatting large JSON files (tested in Windows).
Install Notepad++
Go to Plugins -> Plugins Admin -> Install the 'Json Viewer' plugin. The plugin source code is present in https://github.com/kapilratnani/JSON-Viewer
After plugin installation, go to Plugins -> JSON Viewer -> Format JSON.
This will format your JSON file

How to use bsondump in windows?

How to convert the BSON into JSON from MongoDB.
I inserted documents into MongoDB then I get the following error.
This what I get
C:\mongodb\bin>bsondump collection.bson> collection.json error: boost::filesystem::file_size:
The system cannot find the filespecified:"collection.bson"
please use the absolute directory of collection.bson file or see whether the file is in C:\mongodb\bin or not
Firstly, you have to run the mongodump, which makes the bson file. It creates a dump folder, and it makes for every database an other folder. After that you have to run bsondump coll.bson > coll.json.