Write JSON with Node.js - javascript

I'm trying to push names in a json file. I'm trying to do something like:
socket.on('create json', function(data){
var data = JSON.stringify(Data, null, 2);
fs.writeFile('participants.json', data)
console.log(data);
});
This is only outputting the data that I've send and results in:
{
"type": "Buffer",
"data": [34,69,120,97,109,112,108,101,32,110,97,109,101, 34 ]
}
When I'm writing the file it deletes everything and puts that in.
I'm looking for a way to write:
{
"names":["name1", "name2", "name3"]
}
Any ideas on how to fix and write this?
Help is very much appreciated!

you have to again read your file, parse the JSON, append your new result to the array, transform it back into a string and save it again.
var fs = require('fs')
fs.readFile('participants.json', function (err, data) {
var json = JSON.parse(data);
json.name = ["name1", "name2", "name3"];
fs.writeFile("results.json", JSON.stringify(json))
})

Related

Different actions based upon JSON value in Node

A perplexing question. I am writing a node based command line tool that has a source json file, and then will take a changes JSON file (i.e. add, update, delete, etc) and I need to basically make the operation into a new JSON file and output to a new file. Not as simple as it sounds. With no command line arguments, etc. you would need to have a directive field in the JSON like so?
The app would work like this:
$ myapp source.json changes.json newfile.json
{
"action": "addRecord",
"payload": {
"id": "1",
"name": "James Hetfield",
"guitar": "More Beer"
}
},
"action": "deleteRecord",
"payload": {
"id": "3",
"name": "Dave Mustaine",
"guitar": "Ole Faithful"
}
}
My JSON structure is probably wrong as well, but wondering how you would use JSON.parse, JSON.stringify to read the file in with the fs library and actually make actions happen when identifying action and then execute a CRUD like statement with payload
Here is what I have so far:
#!/usr/bin/env node
// using fs for parsing for small example
const fs = require('fs');
// We do not simply require the file due to scaling.
// Large file will cause event locking. To handle
// even greater files we would use Node streams.
const file = './changes.json';
// target filename for output
const target = 'output.json';
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
console.log('Error' + err);
return;
}
let obj = JSON.parse(data, (key, value) => {
if (key === 'action') {
if (value === 'addSong') {
return value = "Song Added";
}
if (value === "addPlaylist") {
return value = "Playlist added";
}
if (value === "deletePlaylist") {
return value = "Playlist deleted";
}
}
return value;
});
console.dir(obj);
});
fs seems to just read through the file and pick up the last value read. Not good. Wondering how to do the compare and possibly structure the JSON to make the action happen and then append? or transform the JSON into a new file with the update. Stuck.

How can I insert data from a JSON to a const variable in NodeJS for using an API?

I'm currently working on a NodeJS project, this takes data from a JSON and then use it to take weather stuff form an API, after that I want to save it to a DB, I already asked about it and that question helped me fixing some problems but now I have some others, I'm sending the data to a constant but the issue is that I don't know why am I getting an error in the JSON Parse, I want to use the lat and lon from the JSON (I have like a hundred data coords) and insert it into the const, any help would help, This is the error I'm getting
Successful connection
[]
undefined:1
^
SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
here is my function that takes data from JSON and gets data from the API:
async function calcWeather() {
fs.readFile("./json/data.json","utf8", function (err, data) {
if(err) throw err;
data2 = JSON.parse(data);
console.log(typeof data2);
for (let item of data2) {
let base = `https://api.openweathermap.org/data/2.5/weather?lat=${item.latjson}&lon=${item.lonjson}&appid=${api_key}&units=metric&lang=sp`;
fetch(base)
.then((responses) => {
return responses.json();
})
.then((data) => {
var myObject = {
Id_Oficina: item.IdOficina,
Humedad: data.main.humidity,
Nubes: data.clouds.all,
Sensacion: data.main.feels_like,
Temperatura: data.main.temp,
Descripcion: data.weather.description,
};
// validation and saving data to array
if (myObject.Temperatura < 99) {
lstValid.push(myObject);
}
});
}
});
console.log(lstValid);
}
here is the JSON where I take the data:
[
{
"latjson": 1,
"lonjson": 1,
"IdOficina": "1"
},
{
"latjson": 2,
"lonjson": 2,
"IdOficina": "2"
}
]
I think the issue is in the parse, but I don't get what I am doing wrong
Since you are reading the file with fs.readFile, you are getting a string and not a JavaScript object. You would need to parse it entirely in order to be able to manipulate the content (you seem to be parsing the first character only):
const fs = require('fs')
let rawdata = fs.readFileSync('./data.json')
let data = JSON.parse(rawdata)
Personally, I think it's way easier to require it (no need to use fs):
const jsonData = require('./json/data.json')
async function calcWeather() {
for (let item of jsonData) {
// ...
}
}

Working with json array in files in JavaScript for loading, saving, update data?

Basically, this shouldn´t be a very difficult question, but I´ve tried for like 2 or 3 hours and couldnt reach my goal, especially for such an "easy" question. Im using Node.js and my goal is, to load data from a Json file into a variable, add some new data to this and store my data into the same json file again.
Herefor, my json file looks like this:
[
{
"name": "Max",
"date": "1.1.2020"
}, {
"name": "Henry",
"date": "2.2.2020"
}
]
Here´s my code:
const fs = require('fs');
const filename = './jsonFile.json';
const data = loadJSON();
// console.log(data) keeps saying undefined (whithout using .toString in loadJSON)
function loadJSON() {
JSON.parse(fs.readFileSync(filename).toString); // toString doesnt work
}
function saveJSON(data) {
fs.writeFileSync(filename, JSON.stringify(data));
}
function adduser(username) {
var today = "3.3.2020"; // doesnt matter
let obj = {
name: username,
date: today
}
vipJson.push(obj);
saveVIP(vipJson);
}
It doesnt seem to be working. Could anyone help me, to fix my problem, so I can work with .json files ? Thanks a lot!
You need to specify the BufferEncoding options to read the file, something like this.
const fs = require("fs")
const data = fs.readFileSync("./myfile.json", { encoding: "utf8", flag: "r" })
If you are sure about json files, you can read data from file using require.
const fs = require('fs');
const filename = './jsonFile.json';
const data = loadJSON();
function loadJSON() {
return require(filename)
}
function saveJSON(data) {
fs.writeFileSync(filename, JSON.stringify(data));
}
function adduser(username) {
var today = "3.3.2020"; // doesnt matter
let obj = {
name: username,
date: today
}
data.push(obj);
saveJSON(data);
}
Try above code snippet.

How to check in NodeJS if json file already got specific data

I'm trying to make a CRUD operations with NodeJS. But i don't know how to check if JSON file already got the specific part and then update only needed object and not overwrite the other data or add the new data if there is no record of it.
this is what i have so far ( right now the insert operation overwrites the whole file and leaves it with only inserted data ) :
JSON :
JSON:
{
"id": 1,
"name": "Bill",
},
{
"id": 2,
"name": "Steve",
},
Code :
var operation = POST.operation; // POST request comes with operation = update/insert/delete
if (operation == 'insert') {
fs.readFile("data.json", "utf8", function (err) {
var updateData = {
id: POST.id,
name: POST.name,
}
var newUser = JSON.stringify(updateData);
fs.writeFile("data.json", newUsers, "utf8");
console.log(err);
})
}
else if (operation == 'update') {
fs.readFile("data.json", "utf8", function (err) {
})
}
else if (operation == 'delete') {
fs.readFile("data.json", "utf8", function (err) {
})
}
else
console.log("Operation is not supported");
The most examples that i've found were with Database and Express.js. So they didn' really help much.
Sorry , i'm a newbie.
So first off, that is not valid JSON (unless this is only part of the file). You will need to wrap that whole list into an array for it to be valid JSON (You can check if your JSON is valid here)
If you go with a structure like this:
[
{
"id": 1,
"name": "Bill",
},
{
"id": 2,
"name": "Steve",
},
]
I think the easiest way to check if the ID already exists will be to read the JSON in as an array and check if the ID has already been assigned. Something like this:
var json = require('/path/to/data.json'); // Your object array
// In the if (operation == 'insert') block
var hadId = json.some(function (obj) {
return obj.id === POST.ID;
});
if (hasId) {
// Do something with duplicate
}
json.push({
id: POST.id,
name: POST.name
});
// Write the whole JSON array back to file
More on the array.some function
So basically you will be keeping the whole JSON file in memory (in the json array) and when you make a change to the array, you write the whole updated list back to file.
You may run into problems with this if that array gets very large, but at that point I would recommend looking into using a database. Mongo (all be it not the greatest DB in the world) is fairly easy to get setup and to integrate with JavaScript while playing and experimenting.
I hope this helps!
Good luck

How to get JSON Elements from String(full with JSON object).

I have a JSON file
{ "impressions":
[
{
"impressionId": "7ad7a77fas346a7a2a1da6",
"userId": "hafsa",
"clientId": "400"
},
{
"impressionId": "7ad7a77fas346a7a2a1da7",
"userId": "asif",
"clientId": "200"
},
{
"impressionId": "7ad7a77fas346a7a2a1da8",
"userId": "zadarov",
"clientId": "300"
},
{
"impressionId": "7ad7a77fas346a7a2a1da9",
"userId": "julia",
"clientId": "100"
}
]
}
I am working on Kafka and have a NodeJS Producer and Java Consumer.I have to get each impression as a separate message in my consumer. My nodeJS code is:
console.log("Kafka is recieving JSON....");
var fs = require('fs');
var kafka = require('kafka-node');
var Producer = kafka.Producer;
var Client = kafka.Client;
var client = new Client('localhost:2181');
var producer = new Producer(client);
fs.readFile('data.json', 'utf8', function (err, data) {
if (err) throw err;
var jsonobj = JSON.parse(data);
var countMessages = jsonobj.impressions.length;
var impressionArr = [];
impressionArr = jsonobj.impressions;
payloads = [
{ topic: 'test', messages: impressionArr, partition: 0 }
];
producer.on('ready', function(){
producer.send(payloads, function(err, data){
console.log(data);
});
});
producer.on('error', function(err){
console.log("Error: "+err)
});
});
and my JAVA consumer is :
JavaInputDStream<String> messagesFrmSpecifiedOffset= KafkaUtils.createDirectStream(
sc,
String.class,
String.class,
StringDecoder.class,
StringDecoder.class,
String.class,
kafkaParams,
fromOffset,
new Function<MessageAndMetadata<String, String>, String>() {
public String call(MessageAndMetadata<String, String> msgAndMd) throws Exception {
return msgAndMd.message();
}
}
);
and I am getting impressions as separate messages as follows in JAVA consumer.
JavaDStream<Record> rdd_impressionRecords = messagesFrmSpecifiedOffset.map(
new Function<String, Record>() {
public Record call(String impressions) throws Exception {
System.out.println("impressions: " + impressions);
Record sd = new Record();
return sd;
}
});
BUT MY PROBLEM IS:
that I am getting output in object form like:
impressions: [object Object]
impressions: [object Object]
impressions: [object Object]
impressions: [object Object]
I know that I am sending JSON and recieving as String but anyone can help me to do solution in three ways:
1. Can I get key, values from [object Object] in Java class.
2. Is it possible to send each array element as String (using JSON.stringify) in NodeJS without any loop. Direct transfer as I am doing.
3. Can I rewrite method of createDirectStream that returns JSON obect directly.
In addition to Zdeněk Tisoň answer:
a) First use JSON.stringify() to create a string and send it over the net.
b) At the consumers side transform this string to whatever you want. If you want it to be Json you can use JSON.parse().
Client code could be:
var _ = require('lodash');
var payloads = _.map(JSON.parse(data).impressions, function(imp){
return JSON.stringify(imp);
});
producer.send(payloads, function(err, data){
});
Spark code could use json-simple library:
import org.json.simple.*;
JSONObject imp = (JSONObject) JSONValue.parse(impression);
String impressionId = (String) imp.get("impressionId");
You could use Java's JSONObject to get the string as a JSONObject like so:
JSONObject jsonObject = new JSONObject(impressions)
Then use the JSONObject functions to get what you need as listed here:
http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
Use JSON.stringify before sending messages onto the Kafka bus. For example, your code changes to messages: JSON.stringify(impressionArr).
AFAIK producing object to kafka is not supported by kafka-node. If you try to do that, the object itself would be in format of [object Object], actually it's String '[object Object]'. That's why you can not de-serialize from Java.
try JSON.stringify(impressionArr) when produce in your node code and some JSON library when consume from Java side.

Categories

Resources