Club different MQTT Topic data into one JSON string in node red - javascript

How can I club all the MQTT Topic data in one JSON String
I was able to get JSON string for the individual topic only like this
"{"time":1549737900821,"payload":"1997.32","topic":"RotateZ"}"
{"time":1549737900821,"payload":"1954.32","topic":"RotateY"}"
but I want to display all the topic data in one JSON string only for example
"{"time":1549737900821,
"RotateZ":"1997.32",
"RotateY":"1954.32"}"
I am using mentioned below code in function node
var topic = msg.topic;
var d = new Date();
var t = d.getTime();
payload = {"time":t, "payload" : msg.payload ,"topic": topic }
msg.payload = payload;
return msg;
what modification will help me to make it work?
Any suggestion on this will be a great help

Try Join node. Set Manual, Combine each msg.payload to create a key/value object using the value of msg.topic as the key. On output add timestamp in simle function like this :
var d = new Date();
msg.payload.time = d.getTime();
return msg;

Related

JSON response data type is not uniform

I have AJax call as below using JS .I am using Knockout JS
var someManager = new AjaxManager('/YYYYYY/XXX/list');
CommonUtils.dispWaitPopup();
// someManager List addLoadCallback()
someManager.addLoadCallback(function (data) {
self.YYYList = ko.observableArray(data.YYYYList);
var viewModel = new ProjectViewModel(data);
ko.applyBindings(viewModel);
CommonUtils.closeWaitPopup();
});
var startDate = new Date();
startDate.setMonth(startDate.getMonth() - 3);
var endDate = new Date();
var param = {
'startDate': moment(startDate).format("YYYY-MM-DD HH:mm:ss"),
'closeDate': moment(endDate).format("YYYY-MM-DD HH:mm:ss"),
};
someManager.request(param);
Datas are fetched from Postgres DB. For particular Numeric field (Example charging rate at back end is having values as 1.00 but Ajax response simply ignore trailing zeros after decimal point by treating it as Numeric . Ironically for some other AJAX call same data (rate ) is returned as String "1.00". I am very much puzzled how same record is returned as Numeric for one Ajax response as well as String for other AJAX call response. Result is coming from same table and same column value.

Companies House, extracting data from the response (rest API)

I'm using Companies House API with a small google apps script. I want to retrieve the company numbers for a list of companies on a sheet.
I can't seem to be able to access the key/values of the response, can anyone help?
function findPayeeCHInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1")
var optionsheet = ss.getSheetByName('Options');
var payee = sheet.getRange("F6").getValue();
var my_api_key = optionsheet.getRange('C2').getValue();
//Logger.log(my_api_key);
var headers = {
"Authorization": "Basic " + Utilities.base64Encode(my_api_key+":"),
}
var params = {
"method":"GET",
"headers":headers,
muteHttpExceptions: true,
};
var url = "https://api.companieshouse.gov.uk/search/companies?q=" + payee;
var response = UrlFetchApp.fetch(url,params);
Logger.log(response);
}
my logger says:
[20-08-02 16:14:00:624 BST] Logging output too large. Truncating output. {"kind":"search#companies","total_results":4163,"items_per_page":20,"start_index":0,"page_number":1,"items":[{"description":"01026167 - Incorporated on 4 October 1971","snippet":"BARCLAYS BANK INTERNATIONAL ","address_snippet":"1 Churchill Place, London, E14 5HP","matches":{"title":[1,8,10,13],"snippet":[1,8,10,13]},"address":{"postal_code":"E14 5HP","address_line_1":"Churchill Place","premises":"1","address_line_2":"London"},"kind":"searchresults#company","description_identifier":["incorporated-on"],"title":"BARCLAYS BANK PLC","company_type":"plc","links":{"self":"/company/01026167"},"company_number":"01026167","company_status":"active","date_of_creation":"1971-10-04"},{"description_identifier":["incorporated-on"],"address":{"country":"England","locality":"London","premises":"1","postal_code":"E14 5HP","address_line_1":"Churchill Place"},"kind":"searchresults#company","snippet":"BARCLAYS UK AND EUROPE ","address_snippet":"1 Churchill Place, London, England, E14 5HP","matches":{"snippet":[1,8],"title":[1,8,10,13]},"description":"09740322 - Incorporated on 19 August 2015","company_number":"09740322","date_of_creation":"2015-08-19","company_status":"active","company_type":"plc","links":{"self":"/company/09740322"},"title":"BARCLAYS BANK UK PLC"},{"title":"ZEDRA TRUST
i've used different methods of addressing object values and they're either not recognised, return a null value or return everything that you see above.
I wasn't even sure it's an object, so tried addressing it as an array, with zero success. According to the documentation, this is a subset of "items", but when i try to log response.items.company_number i get
TypeError: Cannot read property 'company_number' of undefined –
The resource representation is here:
https://developer.companieshouse.gov.uk/api/docs/search-overview/CompanySearch-resource.html
any pointers will be gratefully received.
UrlFetchApp.fetch() returns HTTPResponse
Get the string from HTTPResponse and parse the string to a object
items in the response is a array of objects. Access the object inside using index of the array and then use company_name key
var httpResponse = UrlFetchApp.fetch(url,params);
var text = httpResponse.getContentText();
var object = JSON.parse(text);
var compName = object.items[0]['company_name'];
Logger.log(compName);

Is there Any Way to Convert Mongo ObjectId into string by javascript/jquery

Is it possible to convert mongo objectId into string.
The above pictures shows data i received and shown in console.I need id value in string form .but ObjectId is returning as object
In Database id is look like this- 565d3bf4cefddf1748d1fc5e -objectId and i need id exactly like this –
According to the Mongo documentation:
a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.
You can check it out here: https://docs.mongodb.org/manual/reference/object-id/
So in javascript you could do something like this.
var mId = {
Timestamp:1448950573,
Machine:13565407,
Pid:1756,
Increment:8888962
};
function getId(mongoId) {
var result =
pad0(mongoId.Timestamp.toString(16), 8) +
pad0(mongoId.Machine.toString(16), 6) +
pad0(mongoId.Pid.toString(16), 4) +
pad0(mongoId.Increment.toString(16), 6);
return result;
}
function pad0(str, len) {
var zeros = "00000000000000000000000000";
if (str.length < len) {
return zeros.substr(0, len-str.length) + str;
}
return str;
}
console.log(getId(mId))
It produces "565d3b2dcefddf06dc87a282" which was not exactly the id you had, but that might just be a tweak or i was working with different data :D.
EDIT
Added a padding function so that zeros are not truncated.
Hope that helps
EDIT:
I assume you are using c# to connect to and serve documents from the mongo DB. In that case, there is a driver that also supports toString().
Here is an example using the mongo csharp driver:
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
// ...
string outputFileName; // initialize to the output file
IMongoCollection<BsonDocument> collection; // initialize to the collection to read from
using (var streamWriter = new StreamWriter(outputFileName))
{
await collection.Find(new BsonDocument())
.ForEachAsync(async (document) =>
{
using (var stringWriter = new StringWriter())
using (var jsonWriter = new JsonWriter(stringWriter))
{
var context = BsonSerializationContext.CreateRoot(jsonWriter);
collection.DocumentSerializer.Serialize(context, document);
var line = stringWriter.ToString();
await streamWriter.WriteLineAsync(line);
}
});
}
ORIGINAL:
These are Mongo ObjectId's and if you haven't already deserialised the document they should support a toString method that will return a hexadecimal string.
but if you want this applied to the whole document, using JSON.stringify(MogoDocument) should deserialize this for you into a plain object.

Locating MAX Number in a string for a list of records

I am relatively new to JavaScript and trying to get the following achievement.
Lets say we have the following data records.
Example data records:
A_ID_R1_V1
A_ID_R1_V2
A_ID_R2_V1
Basically I am looking for two results:
1.Create a string based on record A_ID_R1_V2.
Input = A_ID_R1_V2
Output = A_ID_R1_V3
My thoughts about this is so locate the MAX V<#> for A_ID_R1_ and then add 1.
2.Create a string based on record A_ID_R2_V1.
Input = A_ID_R2_V1
Output = A_ID_R3_V1
My thoughts about this is so locate the MAX R<#> for all records and then add 1.
Thanks a lot in advance!!!!!!!!
I have strong feeling that this is some kind of school homework...
Anyhow, this will do the job, but its not sophisticated nor very effective
function updateId(letter){
var pos = (letter === 'V')?3:2;
var parts = id.split('_');
var num = parseInt(parts[pos].substring(1,parts[pos].length));
parts[pos] = letter+(num+1);
id = parts.join('_');
logVal(id);
}
try this with regex: http://jsfiddle.net/mig1098/wh6n2oqL/
newEntry = function(entry){
var str2 = entry.replace(/\d+$/,'');
var num = parseInt(entry.replace(/A_ID_R(\d+)_V/,''));
num = num+1;
return str2+num;
}

defining date in json file

I wonder to know how to set today's date in json file like we are using in js.
Is there any option to specify Date.today() in json file?
Because, json data has date object which specifies system date whenever we read the json file.
Hope, you will understand what i am trying to say.
Thanks in advance,
-Raja.
Server side can generate JSON dates in ISO format for example "2012-04-30T02:15:12.356Z"
Then client side can parse and load into date object
new Date(Date.parse("2012-04-30T02:15:12.356Z"))
JSON is a structured transport format. It does not have logic in it.
But here are options:
Why not just get the date when you read the file instead?
Have a server generate that JSON that includes the date at which it was generated. However, this is not ideal if you want the current date. By the time you read the file, the date generated by the server is already past.
build a parser that parses a string and make it search for custom markup.
For example, special markup is contained in #{}. Get the command inside, determine the command, and execute replacement.
var jsonString = '{"data1" : "foo","date" : "#{currentdate}"}'
In this case, I'll find #{currentdate}. I should create a function corresponding to this command to replace #{currentdate} into the current date during read (in the format you want)
var parsedString = jsonString.replace(/#\{(\w+)\}/g, function(match, group) {
if (group === 'currentdate') {
return new Date();
}
else if (group === 'anotherCommand') {
return 'anotherValue';
} //and so on
});
and the result is like this:
jsonString = '{"data1" : "foo","date" : "Fri May 04 2012 01:17:07 GMT-0700 (PDT)"}'
I suggest that you consider using the JSON-js (json2.js) parser, because it parses all standard JSON, but also allows you to add custom parse handling logic, called a reviver function, which fits your scenario very well. The basic syntax to invoke the JSON parser with a custom handler looks like this:
var myObject = JSON.parse(myJSONtext, reviverFunction);
Using your example input as a guide, it could be set up to work like this:
var jsonTxt = '[{' +
'"data1": "foo",' +
'"Date": "",' +
'"childs": [{' +
'"data2": "stack",' +
'"Date": ""' +
'}{}{}...]}]'; //and-on-and-on as in your comment
myData = JSON.parse(jsonTxt, function ( key, value ) {
if ( key === 'Date') { return new Date(); }
//any additonal custom logic you may need later...
});
A general introduction to JSON-js is provided at the JSON in JavaScript page, along with some brief intro info about JSON, and the page also includes some usage scenarios.
You can consider leveraging popular library like moment.js http://momentjs.com/
Then you can store date as YYYY-MM-DD in json and let moment handle the parsing:
var dateString = '2012-11-01';
var someday = moment(dateString);
var formattedDate = someday.format('ddd, DD MMM YYYY'); // 'Thu, 01 Nov 2012'
If you want to store the date, I would prefer to store as a String with a format like yyyy/mm/dd hh:mm:ss or something like that, and parse it in a Date object when I want to read it in the language I need.
obj = {
dates : ['2012/04/30 10:14:23', '2012/05/01 05:34:01']
}
I don't understand exactly what you want, with eval methods (it's an ugly practice), you can add a method to puts the actual date in object, and also adds himself at the children and call the method added in the children.
obj = {
data : "foo",
addDate : function() {
this.date = newDate();
if (this.children) {
for (var i = 0; i < this.children.length; i++) {
this.children[i].addDate = this.addDate;
this.children[i].addDate();
}
}
},
children : [{
data : "foo2"
}]
}
PS if you want to store it in a file, then you have to use the eval method (not recommended) storing the methods as a string or evry time you load the file do
jsonLoaded; // this var has the json that you store in a file;
var addDate = function() {
this.date = newDate();
if (this.children) {
for (var i = 0; i < this.children.length; i++) {
this.children[i].addDate = this.addDate;
this.children[i].addDate();
}
}
this.addDate = null; // remove the function we have added
// delete this.addDate; // if is compatible with browser
}
jsonLoaded.addDate = addDate;
jsonLoaded.addDate();
you cannot stringify json objects with functions, because of this, in the second method after add the method addDate, we remove that from the json object (also you can do delete this.addDate, but i don't know if it works in IE)
Wouldn't it be easier just to calculate the current system data whenever you read the file? I may be lacking context here but I don't see the point in storing that in the document.
If you really need to do so you can do as follows
var jsonObject = {"now":"new Date()"};
var date = eval(jsonObject.now);
it will be good to collect all the dates you want to transfer into a
Collection<String> dates = new ArrayList<String>();
Convert this collection to a json object and then at the receving end,convert it back to date. You can use joda date time API for conversions.
I use Gson in java to create json output, but Gson does not allow me to put javascript functions into the json. So this is what I do: Use replacement tags for the places you want to put code(like one of the earlier answers). Then get the text of the json, replace the tags, and then save the text to your json file:
Map<String, String> dynamicDates = new HashMap<>();
dynamicDates.put("d1", "new Date()");
dynamicDates.put("d2", "new Date(2015, 0, 1, 9, 30)");
dynamicDates.put("d3", "new Date(2015, 0, 1, 12, 30)");
JsonObject json = new JsonObject();
JsonObject root = new JsonObject();
JsonObject level_1_A = new JsonObject();
JsonObject level_1_B = new JsonObject();
json.add("root", root);
root.add("level_1_A", level_1_A);
root.add("level_1_B", level_1_B);
level_1_A.addProperty("d1", "${d1}");
level_1_A.addProperty("d2", "${d2}");
level_1_B.addProperty("d3", "${d3}");
StringBuilder sb = new StringBuilder();
new GsonBuilder().setPrettyPrinting().create().toJson(json, sb);
String str = sb.toString();
for (String key : dynamicDates.keySet()) {
str = str.replace("\"${" + key + "}\"", dynamicDates.get(key));
}
String jsonText = str;
String javascriptText = "var myJson = " + str + ";";
System.out.println(jsonText);
System.out.println(javascriptText);
So there is nothing left to be done on the consumption side in using this json. And the first output is:
{
"root": {
"level_1_A": {
"d1": new Date(),
"d2": new Date(2015, 0, 1, 9, 30)
},
"level_1_B": {
"d3": new Date(2015, 0, 1, 12, 30)
}
}
}
My use of json is usually saving it as javascript with an assignment, so this has been working for me.

Categories