Handling JSON in the file

#1

Hi Guys!
I try to work with JSON file to persist important parameters and I using at first moment, to study the best way to handling it, I starting to read a file, to do it i using REQUIRE Method, and I had this error "require is not defined (line 1, col 10)"

My function GetJSON Parameters:

var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('/home/pi/HomeGateway/nwkFilestorage.json', 'utf8'));
msg.payload=obj;
return msg;

My nodes:

[{"id":"8213a1aa.82424","type":"function","z":"36fc56fb.15e79a","name":"GetJSON Parameters","func":"var fs = require('fs');\nvar obj = JSON.parse(fs.readFileSync('/home/pi/HomeGateway/nwkFilestorage.json', 'utf8'));\nmsg.payload=obj;\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":120,"wires":[["136175e2.bd12fa"]]},{"id":"136175e2.bd12fa","type":"debug","z":"36fc56fb.15e79a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":660,"y":120,"wires":[]},{"id":"2a056dd2.f9a382","type":"inject","z":"36fc56fb.15e79a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":90,"wires":[["8213a1aa.82424"]]}]

The error:
I have checked "setting.js" and I didn´t see anything to fix at the first moment.
:slight_smile: 36

My nodes:


My Settings:

Can someone have any idea how to fix it?

Best Regards,
Alex

1 Like

#2

Hi @aargollo

you cannot use require in a Function node.

You have correctly added fs to your functionGlobalContext setting, but you are missing the next piece from https://nodered.org/docs/writing-functions#loading-additional-modules which explains how to then access that module in your code.

var fs = global.get('fs');
0 Likes

#3

But I have to ask why not use the file node to read the file directly ?

0 Likes

#4

Thanks dceejay, I have to use node to ready the file directly. Thanks!
Hi Knolleary, thanks right now is working!!!

I have to use, before storage in the file, check if the device exist through JSON object property called "Nwk_IEEEAddrS" ! To do it I using the code bellow, right now I´m sure that I got success to check if file exist, but I have difficult to find an object parameter in the file.

var fs = global.get('fs');
fs.readFile('/home/pi/HomeGateway/devices.json', 'utf8',function (err, data) {

        /*obj = JSON.parse(data);
        console.log(obj);*/
        if (err) {
            throw err;
        }
        else{
            if (data === ""){

                console.log('data does not exsist');


            }
            else{
                console.log('exsist data');


                
                }
        }

});

my JSON file - devices.json:

devices.json content file

Nwk_IEEEAddrS  10137a101011d94d
[{"Device":"ZBALRM","IEEEAddr":"0137a000001d94d","NwkAddr":"c0c3","EP1":"01","IEEEAddrs":"10137a101011d94d","NwkAddrs1":"c0","NwkA
ddrs2":"c3","EP1s":"11","Desc":"Zigbee device actuators, lamps or secrurity things"}]
Nwk_IEEEAddrS  10137a1010109a99
[{"Device":"ZBHT-2","IEEEAddr":"0137a0000009a99","NwkAddr":"f183","EP1":"0a","IEEEAddrs":"10137a1010109a99","NwkAddrs1":"f1","NwkA
ddrs2":"83","EP1s":"1a","Desc":"Zigbee device actuators, lamps or secrurity things"}]
Nwk_IEEEAddrS  10137a1010109a99
{"Device":"ZBHT-2","IEEEAddr":"0137a0000009a99","NwkAddr":"f183","EP1":"0a","IEEEAddrs":"10137a1010109a99","NwkAddrs1":"f1","NwkAd
drs2":"83","EP1s":"1a","Desc":"Zigbee device actuators, lamps or secrurity things"}

I Would like to find if Nwk_Addr exist using "Nwk_IEEEAddrS" , ex "10137a1010109a99", but I have doubts how to find it in the file.
Anybody can help me, please?

0 Likes

#5

Hi @aargollo

how is that file being generated? It is not a proper JSON file which will make your task harder than it could be.

If you are not able to change the format of the file to be valid JSON, then you'll have to do something along the lines of:

  1. once you've loaded the file content, split it into individual lines:

     var lines = data.split("\n");
    
  2. scan through the lines array looking for the address you are interested in. Once you find it, return the next line as that presumable contains the information about the device you are looking for

    var theAddress = "10137a1010109a99";
    for (var i=0;i<lines.length; i++) {
        if (lines[i].indexOf(theAddress) > -1) {
          // Found it, skip to next line.
          i++;
          break;
        }
    }
    if (i < lines.length) {
        msg.payload = JSON.parse(lines[i]);
        return msg;
    }
0 Likes

#6

Hi My friend, thanks a lot for your help!
Right now is working well. I´ll working on to improve my JSON file as you advise me!

Best Regards,
A|ex

0 Likes

#7

Hi Knolleary,
I working on my JSON file to fix the correct format, as you can see bellow:

Note: I did not implement at this moment the code to prevent duplicated record!

0 Likes

#8

Hi guys!
I have progress to working with device object, I have using flow.context and after it I have a perfect JSON file with unique device as object and parameters.

As I told my device parameters is building in the flow cyclically, to ensure that all those parameters will be part of device object I included a delay node and than storage all those in the context flow. After it I have a perfect JSON file for future use. As you can see below:

image

I using this code, in Context Definition function, to persist my device object, using a unique ID (ext_addr) as you see below:

var unique = String(msg.payload.name);
var Epcount = global.get('EP_Count');
var prop1 = global.get('nwk_addr1');
var prop2 = global.get('nwk_addr2');
var prop3 = global.get('ZDP_EP1');
var prop4 = global.get('ZDP_EP2');
var prop5 = global.get('attr.str');
var prop6 = global.get('ext_addr1');
var prop7 = global.get('ext_addr2');
var prop8 = global.get('ext_addr3');
var prop9 = global.get('ext_addr4');
var prop10 = global.get('ext_addr5');
var prop11 = global.get('ext_addr6');
var prop12 = global.get('ext_addr7');
var prop13 = global.get('ext_addr8');

if (Epcount ==1) {
    
    var properties = {
     "nwk_addr1":prop1, 
     "nwk_addr2":prop2, 
     "EP1":prop3,
     "ModelID":prop5,
     "Ext_addrb1":prop6,
     "Ext_addrb2":prop7,
     "Ext_addrb3":prop8,
     "Ext_addrb4":prop9,
     "Ext_addrb5":prop10,
     "Ext_addrb6":prop11,
     "Ext_addrb7":prop12,
     "Ext_addrb8":prop13,
     
};

} else if (Epcount ==2) {

    var properties = {
     "nwk_addr1":prop1, 
    "nwk_addr2":prop2, 
    "EP1":prop3,
    "EP2":prop4,
    "ModelID":prop5,
    "Ext_addrb1":prop6,
    "Ext_addrb2":prop7,
    "Ext_addrb3":prop8,
    "Ext_addrb4":prop9,
    "Ext_addrb5":prop10,
    "Ext_addrb6":prop11,
    "Ext_addrb7":prop12,
    "Ext_addrb8":prop13,
    };

}

node.send({
 "ext_addr":unique,
 "properties":properties});
flow.set(unique,
 {"ext_addr":unique,
 "properties":properties});

I would like to use a context file system to get, in other flows, some parameters like nwk_addr1, nwk_addr2 and other parameters!I can use memory or file system data to working on it! I think that in the memory would be better to use!

image|499x305

Data context flow:
image

Somebody have an example code or article links to help me how to handling the JSON file to get (via filter) some parameters?
@Steve-Mcl, @Colin, @dceejay and @knolleary. Please, they have a good idea to help me?

0 Likes