# Handling JSON in the file

**URL:** <https://discourse.nodered.org/t/handling-json-in-the-file/7823>\
**Category:** General\
**Created:** [11 February 2019 09:56 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823 "2019-02-11T09:56:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [11 February 2019 09:56 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/1 "2019-02-11T09:56:31Z")

</div>

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:**

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

```

**My nodes:**

```auto
[{"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.  
🙂 ![36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e506631953555a1437ede8d48a00f52c03250b5a.png)

**My nodes:**

 ![54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/da1210dec5f88307fe8c0d59bdb7a15d57177b28.png)  
**My Settings:**  
 ![37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/49b199b1e8ac6e050dd7adcd580c33586f285b56.png)

Can someone have any idea how to fix it?

Best Regards,  
Alex

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 February 2019 09:58 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/2 "2019-02-11T09:58:47Z")

</div>

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](https://nodered.org/docs/writing-functions#loading-additional-modules) which explains how to then access that module in your code.

```javascript
var fs = global.get('fs');

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 February 2019 17:55 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/3 "2019-02-11T17:55:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [13 February 2019 00:24 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/4 "2019-02-13T00:24:45Z")

</div>

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.

```auto
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:

 ![13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/aa7f1c55141a87145e474aa0b59b66884f12fc6a.png)

devices.json content file

```auto
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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [14 February 2019 00:38 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/5 "2019-02-14T00:38:13Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [14 February 2019 10:00 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/6 "2019-02-14T10:00:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [18 February 2019 16:49 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/7 "2019-02-18T16:49:25Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bdd130fc36cdc5cc5223706d285f1de7c9bccace.png)

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

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [15 March 2019 01:27 UTC](https://discourse.nodered.org/t/handling-json-in-the-file/7823/8 "2019-03-15T01:27:16Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7565250795fb08831745e53b9452738310087864.png)

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

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/068b66bb0933d204e5f5b8e11e24250d32d84294.png)

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?
