CSV as a signle line string with newlines - javascript

I have an existing service to create files from strings and zip them up. It currently takes takes JSON as a string as such:
data: ${ JSON.stringify(data) }
which works. However I want to in the came way express that data as a csv.
assume the data is:
json
[
{
"test1": "1",
"test2": "2",
"test3": "3",
},
{
"test1": "1",
"test2": "2",
"test3": "3",
},
]
I have found many good node json to csv libraries, I ended up using json2csv and have found that I can successfully create a csv file for one line of data, but not two as follows.
// works (header row)
const test = "\"test1\",\"test2\",\"test3\"";
const csvStr = `data:text/csv;charset=UTF-8, ${ test }`;
// fails (header row + 1 data row)
const test = "\"test1\",\"test2\",\"test3\"\n\"test1\",\"test2\",\"test3\"";
const csvStr = `data:text/csv;charset=UTF-8, ${ test }`;
Based on these tests I believe the issue is with how the newline / carriage return is being used. If this is even possible. Anyone know what I might be doing wrong here? If it's possible to express a CSV file on a single line with line breaks?

Related

quick question about JSON new line to javascript

Basically I have a JSON file and whenever I take it to my js application and print it to the console it never print a new line I have no Idea why
my JSON file:
[
{
"id": "71046",
"question": "What is a cpu?",
"answer": "Brain of the computer, it has all the circuity needed to proccess input, store data and output results.",
"options": [""]
},
{
"id": "63888",
"question": "What can the proccessor perform?",
"answer": "1) Basic arithmetic 2) Logic Control 3) Input/Output",
"options": []
},
{
"id": "5418",
"question": "CPU principle components:",
"answer": "1) ALU (Arithmetic Logic Unit)\n 2) Processor Register\\n 3) Control Unit",
"options": []
}
]
I tried many solution to parse print and others and didn't work which is weird
I went through this link in stackoverflow and didn't find solution: How do I handle newlines in JSON?
Here what happens when I stringify (the \n or \n doesn't take it to another line):
That's probably because you don't "print JSON", but "print a JS object"
Basically, what you would get if the console would be in a browser and not in nodejs is
To print JSON as JSON you must convert it back to text with JSON.stringify
JSON.stringify(
{
"id": "71046",
"question": "What is a cpu?",
"answer": "Brain of the computer, it has all the circuity needed to proccess input, store data and output results.",
"options": [""]
}
),
/*replacer(advanced)*/
undefined,
/*indent as <string> of spaces <number>*/
2
)
Each valid JSON is a valid JS
What you are trying to get is
let j = "multiline
string"
which is obviously invalid in JS or JSON
This is caused by JSON stringifying strings.
Stringified strings can't contain newline, JSON allows newlines
For example:
const str = `
- multiline
- string
`
const json = JSON.stringify(str)
// expected = '" \n- multiline \n- string \n"' - no newli
const expected = '"\\n- multiline\\n- string\\n"'
console.log(json == expected)
console.log(json.includes('\n')) // false, does not contain newlines
try {
console.log(JSON.parse('"asi\nzxc"'))
} catch(err) {
/**
* Unexpected token
* in JSON at position 4
*/
console.log(err)
}
/**
* {
* "json": "\"\\n- multiline\\n- string\\n\""
* }
*/
console.log({json})
JSON.stringify(str)

Create and Read JSON file

I created a JSON file as follows
{
"fooditems" : [
{
"name": "pizza",
"type": "fastfood",
"price": 10
},
{
"name": "apple",
"type": "fruit",
"price": 1
}
]
}
created a JS file to read the JSON file
const data = require("./data.json");
data1 = JSON.parse(data);
data1.foodData.forEach( foodItem => console.log(foodItem));
When I run the JS, I get error for the json file
Syntax error: Unexpected token o in json at position 1
at JSON.parse
You don't need to parse data since it's already and object. The following should work.
const data = require("./data.json");
data.fooditems.forEach( foodItem => console.log(foodItem));
Note foodData was change to fooditems based on the contents of the data.json file.
Your initial data JSON contains "fooditems", but in the JS file you are trying to process the "foodData". Change the "foodData" to "fooditems" and it should work.
I think that you are trying to access invalid object key in your JS file on the last line.
Instead of
data1.foodData
put
data1.fooditems

cytoscape.js - how to merge attributes of the same node from different sources

I have a working graph which displays some nodes & their attributes. Then I get a JSON with different data, where some nodes may already exist on my graph. How to combine both data sources, to make them both visible on the same graph - BUT nodes with the same ID must be combined into one and contain attributes from both data sources (not just from one, as is by default)?
Example:
Node from source 1 => "id": "1", "name": "1", "param1": 100;
Node from source 2 => "id": "1", "name": "1", "param2": 200;
What I wish to see on the graph is one node with attributes:
"id": "1", "name": "1", "param1": 100, "param2": 200
I'm in the middle of writing code in my own application to do exactly what you're asking. The code below works, though I suspect that it's not the most efficient way. So, please don't accept this answer without waiting at least a few days for someone more experienced to post a better answer or to add a comment criticizing this answer.
The trick is to query cy (the cytoscape.js core object) for a "collection object" containing just the node with the givenĀ id, and then query the collection object to see if it's empty. If the node doesn't exist, you cy.add() it. If the node does exist, you call node.data() on the collection object to update it.
function updateGraph(g) { // g is the output from JSON.parse(), containing graph from server
gg = g; // save pointer to g, for console debugging
// Import nodes from server graph
for (const sn of g.nodes) { // sn = node as represented on the server
var node = cy.$id(sn.id) // node = cytoscape.js's representation of node
if (node.empty()) {
node = cy.add({group: 'nodes', data: {
id: sn.id,
label: sn['display-name'], // peculiar to my application
parent: sn.memberOf // peculiar to my application
/* . . . and whatever other data you want to copy to the cytoscape.js graph . . . */
}});
node.addClass(sn.class);
} else {
/* Update `node` with data from `sn`.*/
node.data( /* your overriding data goes here */ );
}
}
}
var gg; // We save the last graph dict from the server here so we can look at
// it in the Chrome debugger even after updateGraph() returns.
The gg variable isn't necessary, of course, but I've found it indispensible for seeing what's going on in the Chrome debugger.
In your application, you might be able to call Object.assign() to merge the data before calling node.data(). That would be simpler and more efficient than my code above, where data from the source has different keys than the keys expected by cytoscape.js.
//Node from source 1 => "id": "1", "name": "1", "param1": 100;
var xNode={"id": "1", "name": "1", "param1": 100}
//Node from source 2 => "id": "1", "name": "1", "param2": 200;
var yNode={"id": "1", "name": "1", "param2": 200}
// finalNode=Object.assign({},xNode,yNode)
var finalNode={...xNode,...yNode}
console.log('merge Obj:'+JSON.stringify(finalNode))

How to find length of JSON file in javascript using jsonfile npm

I have a JSON file that I am using javascript to read, I am able to print it out in console, but I have to manually code the number of objects in the JSON file, the file is really simple with only 3 objects, I would like to create a function that checks how many objects are in JSON file.
Json Code
{
"items": [
{
"fname": "Kali",
"lname": "Flower",
"age": "19"},
{
"fname": "JD",
"lname": "Wyatt",
"age": "19"
}]
}
I'm trying to write a javascript function showing how many objects are in it
Use JSON.parse to convert the content to object then use .length to get the size:
JSON.parse(fs.readFileSync(file)).items.length
Explained:
const fs = require("fs"); // require fs
const content = fs.readFileSync(file); // read the file content as string
const obj = JSON.parse(content); // convert string to object
const length = obj.items.length;

How to add a json object to a json array in an external file in javascript / capserjs

I am writing to a json file in casperjs and am trying to add new objects to it.
json file looks like
{ "visited": [
{
"id": "258b5ee8-9538-4480-8109-58afe741dc2f",
"url": "https://................"
},
{
"id": "5304de97-a970-48f2-9d3b-a750bad5416c",
"url": "https://.............."
},
{
"id": "0fc7a072-7e94-46d6-b38c-9c7aedbdaded",
"url": "https://................."
}]}
The code to add to the array is
var data;
if (fs.isFile(FILENAME)) {
data = fs.read(FILENAME);
} else {
data = JSON.stringify({ 'visited': [] });
}
var json = JSON.parse(data);
json.visited.push(visiteddata);
data = JSON.stringify(json, null, '\n');
fs.write(FILENAME, data, "a");
This is starting off by adding an new { "visited" : [ ] } array with first couple of objects, below the existing { "visited" : [ ] } array and subsequently the script breaks because the json array is no longer valid json.
Can anybody point me in the right direction. Thank you in advance.
You have a JSON file containing some data.
You:
Read that data
Modify that data
Append the modified version of that data to the original file
This means the file now has the original data and then, immediately after it, a near identical copy with a little bit added.
You don't need the original. You only need the new version.
You need to write to the file instead of appending to it.
Change the 'a' flag to 'w'.

Categories