Copied JSON data shows as string in node js / javascript - javascript

I wrote a code where I am copying a one json file into another json file so my code look like this
function userData(){
fs.copyFile(
path.join(process.cwd(), "user/access.json"),
path.join(process.cwd(), "client/access.json"),
(err) => {
if (err) throw console.log(err.message);
}
);
}
so my json structure look like this
{
"Data":{
"users":[
{},
{},
{},
]
}
}
So I am reading json the copied json file like this
fs.readFile(path.join(process.cwd(), "client/access.json"), 'utf8', function(err, data){
console.log(typeof data); // showing as a string as it should shown me as object
// console.log(data.Data.users); // showing undefined
});
but the problem is the typeof of data showing me string as it should be shown as Object so I can read the data.Data.users but now I can't read this any solution whats happening here?

What you read file file is always a string. To convert it to an JSON object you need to do JSON.parse
fs.readFile(path.join(process.cwd(), "client/access.json"), 'utf8', function(err, data){
console.log(typeof data); // showing as a string as it should shown me as object
const obj = JSON.parse(data);
console.log( typeof obj);
// console.log(data.Data.users); // showing undefined
});

Related

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) {
// ...
}
}

Get json data from object from get Storage Amplify

I have some data from an object from an S3 Bucket via Amplify.
export function amplify() {
let key = "68a92d44-f25a-4bd8-9543-cc95369ae9a0";
return Storage.get(key + ".json", {
download: true
})
.then(function(result) {
return result;
})
.catch(err => console.log(err));
}
And the resulting data looks like this:
Object { LastModified: Date Tue Nov 12 2019, ETag: "\"(lotsofnumbers)\"", ContentType: "application/json", Metadata: {}, Body: Uint8Array(4168) }
How would I go about getting the JSON data from this object?
I have tried result.Body.toString() which gives me the JSON file and its content, but I cannot write result.Body.toString().name or .meta (the content in my jsonfile), for example, it gives me "undefined". I have also tried to convert the Uint8Array to JSON with parse, whereas I get this error: "JSON.parse: unexpected character at line 1 column 1 of the JSON data".
This worked for me, don't know what was wrong, but now it works :)
var obj = amplify();
obj.then(async function(result) {
var json = await new Response(result.Body).json();
console.log(json.meta);
});

JSon File first read as an object then as a Json

So my problem is pretty simple.
I have a Json File which contain : { "message": [] }
In this File I'll add continuously messages by parsing it, pushing an item and "stringifying" it back.
Like this :
jf.readFile(file, 'utf8', function(err, json){
json = JSON.parse(json)
json.message.push({user: message.username, date: message.hour, message: message.text})
json = JSON.stringify(json)
jf.writeFile(file, json, 'utf8');
}
However when it is the first message basicJsonF is consider as an [object Object].
And as soon as I have a message in it is consider as a JSon file.
How could I (without an if condition) specify basicJsonF as a Json File?
Edit :
I've found out a solution I guess is clean if anyone need it :
typeof(json) === 'object' ? null : json = JSON.parse(json)
This Code Snippet works fine for what you wanna do. You only need to write the json into your file.
var message = { username: "TestUser", hour: new Date(), text: "HEY THERE!" };
var jsonBasicFile = '{ "message": [] }';
console.log(jsonBasicFile);
var obj = JSON.parse(jsonBasicFile);
console.log(obj);
obj.message.push({user: message.username, date: message.hour, message: message.text});
console.log(obj);
var json = JSON.stringify(obj);
console.log(json);

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.

Not able to pass an object in writeFile method

I want to write into json file .I want to write an object that i am passing
Here is the code
path.exists(logfile_name, function(exists) {
if (!exists) {
var jsonObject={ "req": req,
"result": result ,
"fields": fields } ;
fs.writeFile(logfile_name ,jsonObject,function(err){
if(err){
console.log("error is: " + err)
}
else
console.log("no error found");
});
}
});
In logfile_name it writes
[object Object]
But i want it to write like this
{
"req": value,
"result": value ,
"fields": value}
If you don't pass a string or a buffer to writeFile, the toString function of what you pass is called. In your case it returns "[object Object]".
You have to convert it yourself :
fs.writeFile(logfile_name, JSON.stringify(jsonObject), function(err){
I would advise against naming a JavaScript object variable "jsonObject" : it might create confusion between what is an object and what is some JSON (i.e. a string holding the serialization of an object).

Categories

Resources