Angular get value of uploaded json - json

I have created a Webapp where the user can upload a json. Afterwards it should print out the content of the json. Unfortunately i receive always an empty array.
Here is my JSON:
"06" : {
"fallbackLabel" : "Adelholzener Gastro Classic mit Gas",
"price" : "0.30",
"name" : "0,25l"
},
"07" : {
"fallbackLabel" : "Adelholzener Gastro Classic ohne Kohlensäure",
"price" : "0.30",
"name" : "0,25l"
}
and here is my code:
onFileSelected(event){
this.selectedFile = event.target.files[0];
this.http.get(this.selectedFile).subscribe(data => {
console.log(this.selectedFile.text());
})
}

HttpClient only works with urls : you can't pass a file to the get method (https://angular.io/api/common/http/HttpClient#get).
You must use FileReader :
onFileChanged(event) {
this.selectedFile = event.target.files[0];
const fileReader = new FileReader();
fileReader.readAsText(this.selectedFile, "UTF-8");
fileReader.onload = () => {
console.log(JSON.parse(fileReader.result));
}
}

Related

Parse JSON Postman response

I have a test in Postman where I do a post request and need to parse the json response
The response looks like this:
"aPIProxy" : [ {
"name" : "SFDC-UpdateLoginTime-v1",
"revision" : [ {
"configuration" : {
"basePath" : "/",
"steps" : [ ]
},
"name" : "1",...some attributes}]
and i need to get something like :
"name" : "SFDC-UpdateLoginTime-v1"
"name" : "1"
for a multiple occurrence json file.
The below postman script might help you.
var jsonData = JSON.parse(responseBody);
var jsonNamesData = jsonData.aPIProxy;
console.log(jsonNamesData);
var parsedData = "";
for(var i=0;i<jsonNamesData.length;i++){
parsedData = parsedData +"\"name\" : \"" +jsonNamesData[i].name+"\", ";
console.log("\"name\" : \"" +jsonNamesData[i].name+"\"");
}
console.log(parsedData);
postman.setEnvironmentVariable("parsedNamesResponse", parsedData); // updating parsed data to the environment variable parsedNamesResponse
You could capture multiple 'name' properties using the _.map() function of Lodash, which is a built it module on the native application. I've had to modify what you need slightly as the name key would have been a duplicate.
const result = _.map(pm.response.json().aPIProxy, data => ({
name: data.name,
revisionName: data.revision[0].name
}))
pm.environment.set("response", JSON.stringify(result))
This would then store all the values in an environment variable for you to use elsewhere in another request.
You should first parse the response using JSON.parse, then you can iterate on the parsed object like:
var resObj = JSON.parse(pm.response.text())
for(var i=0; i< resObj.length; i++) {
console.log("name: "+ resObj[i].name);
}

NodeJS JSON.parse convert the char ! into?

i have a problem with JSON.parse() in a NodeJS programm usging Express.
The problem is that when i get an external json using http.get for example like this:
[ { "Name" : "Parachutes", "Artist" : "Coldplay"}, { "Name" : "Lost!", "Artist": "Coldplay" } ]
When i do JSON.parse(ResponseStr) the i get the following json
[ { "Name" : "Parachutes", "Artist" : "Coldplay"}, { "Name" : "Lost?", "Artist": "Coldplay" } ]
The char ! is converting into ? when i do that JSON.parse, i want to have the same output as the original Name because when i want to use that name in a http.get('/example.com/album/Lost?') it is returning to me a 404 error.
This is mi my code:
function httpget(url,callback)
{
var str = "";
http.get(url, function(resp){
resp.on('data', function(chunk){
str += chunk;
});
resp.on('end', function(){
var obj = JSON2.parse(str);
callback(obj);
});
});
}
Edit 1:
I already using resp.setEncoding('utf8'); in my code, the problem is in in the JSON2.parse() (is same as JSON.parse()), before that i can do console.log(str) and the output will show "Lost!" but when i do JSON.parse() the output is like this "Lost?"

How to convert a MongoDB document to JSON Object

I am trying to make a post request with the MongoDB document returned from find query, as the request body in NodeJS.But on the server I'm getting the Error : Invalid JSON. Below is the document that I'm trying to POST
{
"_id" : ObjectId("5739a6bf3f1b41477570dc89"),
"taskCount" : 2,
"study" : "cod",
"phase" : "mansa2",
"rhimeTaskId" : "5739a6bec4567f6e737fd3db",
"recordId" : "5726f3cfc4567f6e737fc3ab",
"recordStudy" : "codstudy",
"recordPhase" : "mansa2",
"recordLanguage" : "Punjabi",
"recordScript" : "Latin",
"_state" : "CodingComplete",
"tasks" : [
{
"physician" : ObjectId("5739a6bd3f1b41477570dc78"),
"stage" : "Coding",
"result" : {
"cod" : "C15",
"feedback" : {
"narrativeLength" : "Adequate",
"positiveSymptomsIncluded" : "Only Positive",
"certainty" : "High"
},
"keywords" : [
"52 yr male, died of food pipe cancer, suffered pain upper abdomen, investigated,FNAC confirmed Cancer, Put on Chemotherapy, multiple cycles, died at home, had fever with chills occasionally"
]
}
},
{
"physician" : ObjectId("5739a6bd3f1b41477570dc79"),
"stage" : "Coding",
"result" : {
"cod" : "C15",
"feedback" : {
"narrativeLength" : "Inadequate",
"positiveSymptomsIncluded" : "Only Positive",
"certainty" : "High"
},
"keywords" : [
"severe pain abdomen, ultrasonography revealed food pipe cancer, chemotherapy given, died"
]
}
}
],
"__v" : 2
}
and here is the code that I wrote to make the POST request
var MongoClient = require('mongodb').MongoClient;
var request = require('request');
var assert = require('assert');
var cmeprovisioning= 'mongodb://localhost:27017/cmeprovisioning';
MongoClient.connect(cmeprovisioning, function(err, db) {
assert.equal(null, err);
var count=0;
console.log("Connected to cmeprovisioning");
var cursor =db.collection('rhimeReport').find(
{"study":"cod","phase":"mansa2","recordStudy":"codstudy",
"recordPhase":"mansa2","_state":"CodingComplete"
});
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
console.dir(doc);
count=count+1;
request({url: "http://cme.host.net:8081/cme-provisioning/update",
method: "POST",json: true,
headers: {"content-type": "application/json"},
json: doc
},function(e,r,b){
console.log("POST Error "+count+" "+e)
console.log("POST Response "+count+" "+r)
console.log("POST BODY "+count+" "+b)
});
} else {
console.log("Some Error : "+err)
}
});
});
I also tried using JSON.stringify(doc), but still got the Invalid JSON error. Is there a way I can use mongo document returned by the find query and convert it to JSON to make the POST request.
I think those ObjectID is what making it an invalid JSON document.
Here's the actual answer:
If you want to convert a mongo object to JSON object.
There's a utility method in every mongo object toJSON
So you can simply do mongoResponseObject.toJSON() on the response object.
e.g.
Products.findById(id).then(res => {
const jsonRes = res.toJSON();
// Here jsonRes is JSON
})
Alternatively you can directly get the JSON object by using the .lean() like this.
Products.findById(id).lean().then(res => {
// Here res is JSON
})
you need to convert object id to string ie.
var result = {
"_id": ObjectId("5739a6bf3f1b41477570dc89"),
"taskCount": 2,
"study": "cod"
};
//now convert to string
result=result._id.toString();
//now you can use the result
Try this,
var cursor =db.collection('rhimeReport').find(
{"study":"cod","phase":"mansa2","recordStudy":"codstudy",
"recordPhase":"mansa2","_state":"CodingComplete"});
cursor.toString();
......
Hope this help.
Try this in robomongo
var cursor = db.getCollection('X').find({},{})
while(cursor.hasNext()) {
print(JSON.stringify(cursor.next()))
}

Node JS : Conversion from JSON to CSV

I have a JSON like this,
{
"Name" : "Gokul",
"PhoneNumber" : 9876543210,
"Qualification" : "BE"
}
I need to convert it to CSV and the converted CSV should not have JSON keys, I need to convert only JSON values to CSV and my result should be like this,
["Gokul",9876543210,"BE"]
Using Node JS, I need to this Conversion.
You could install lodash (npm install lodash) and use _.values:
var _ = require('lodash');
var json = { "Name" : "Gokul", "PhoneNumber" : 9876543210, "Qualification" : "BE" };
var values = _.values(json); // It will be ["Gokul",9876543210,"BE"]
var json = { "Name" : "Gokul", "PhoneNumber" : 9876543210, "Qualification" : "BE" };
var arr = Object.keys(json).map(function(key,index) {
return json[key];
});
console.log(arr);
//logs [Gokul,9876543210,BE]
console.log(arr.join(','))
//logs Gokul,9876543210,BE

How do you insert an array of object into Mongodb node.js

Basically i want to insert into my Mongodb collection called "Events" an array of objects called "Events" with an id for each entry into the array.
This is the result i want to get in json:
{
"events" : [
{
"_id" : ObjectId("53a99cc608ad49712a830081"),
"eTitle" : "Freshers Fair",
"eDesc" : "Bring some friends with you oh wait, you have non ha !",
"eDate" : "2014-06-19 11:20",
"eLink" : "http://fair.com",
"eEvent" : "NEFS"
},
{
"_id" : ObjectId("53a99cc608ad49712a830082"),
"eTitle" : "Blahh",
"eDesc" : "Blah fdinidf !",
"eDate" : "2014-06-19 11:20",
"eLink" : "http://google.com",
"eEvent" : "NEFS"
}
]
}
So far this is the result i have:
[
{
"_id":"53a9b5ed745363432d823d7a",
"eTitle":"jrnfdeiujn rd",
"eDesc":"grfdsreds",
"eDate":"2014-07-05 22:33",
"eLink":"reser",
"eEvent":"Victoria Center"
},
{
"_id":"53a9b771745363432d823d7b",
"eTitle":"Hello worlds",
"eDesc":"blah",
"eDate":"2014-07-20 22:33",
"eLink":"http://google.com",
"eEvent":"social"
}
]
This is how i insert data with node.js:
// Set our collection
var collection = db.get('Events');
// Submit to the DB
collection.insert({
"eTitle" : eTitle,
"eDesc" : eDesc,
"eDate" : eDate,
"eLink" : eLink,
"eEvent" : eEvent
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("eventlist");
// And forward to success page
res.redirect("eventlist");
}
});
So please i how do i make the format look this the first json format i provided. sorry for the nooby question, just started learning node ! Thanks alot
UPDATE
To post Events:
router.get('/eventlist', function(req, res) {
var db = req.db;
var collection = db.get('Events');
collection.find({},{},function(e,docs){
console.log(docs);
res.send(docs);
// res.render('eventlist', {docs: JSON.stringify(docs), title: 'Test'});
});
});
You can do this:
collection.find({},{},function(e,docs){
var doc = { Events : docs };
console.log(doc);
res.send(doc);
// res.render('eventlist', {docs: JSON.stringify(docs), title: 'Test'});
});