# Get everything stored in global context as object

**URL:** <https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883>\
**Category:** General\
**Created:** [3 July 2019 19:04 UTC](https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883 "2019-07-03T19:04:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jovee](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jovee](https://discourse.nodered.org/u/jovee)\
**Post date:** [3 July 2019 19:04 UTC](https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883/1 "2019-07-03T19:04:59Z")

</div>

Hi all!

I'm wondering if there's a way to quickly retrieve all key/value pairs from global context as a single object.

I know that I could change the settings.js file to store context to a file as opposed to memory only, and then use the file-in node to read that file, which does it. But, I would love to know if there was an internal way, so to speak.

Thanks!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 July 2019 19:08 UTC](https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883/2 "2019-07-03T19:08:13Z")

</div>

This will give you the property (key) names of all of the global variables:

```auto
[
    {
        "id": "6642c47c.99bd3c",
        "type": "inject",
        "z": "106a5b82.ef95a4",
        "name": "Do",
        "topic": "List_All_Global_Properties",
        "payload": "",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 150,
        "y": 1229,
        "wires": [
            [
                "f378e359.0c872"
            ]
        ]
    },
    {
        "id": "f378e359.0c872",
        "type": "function",
        "z": "106a5b82.ef95a4",
        "name": "List all context.global properties",
        "func": "/*\nmsg.payload = {\n mqttWsPort: context.global.mqttWsPort,\n nrPort: context.global.nrPort,\n 'delim': context.global.path.delimiter\n};\n*/\n//msg.payload = context.global;\nmsg.topic = \"List/Context-Global\";\nmsg.payload = Object.getOwnPropertyNames(context.global).sort();\nreturn msg;",
        "outputs": 1,
        "x": 358,
        "y": 1229,
        "wires": [
            [
                "8ce935af.7316c8",
                "7bb54311.844abc"
            ]
        ]
    },
    {
        "id": "8ce935af.7316c8",
        "type": "debug",
        "z": "106a5b82.ef95a4",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 628,
        "y": 1229,
        "wires": []
    }
]

```

You should be able to work the rest out from there. `Object.keys` gives you the properties, `Object.values` gives you the property values.

---

<div class="post-metadata">

**Author:** ![jovee](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jovee](https://discourse.nodered.org/u/jovee)\
**Post date:** [3 July 2019 20:03 UTC](https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883/4 "2019-07-03T20:03:30Z")

</div>

[quote="jovee, post:3, topic:12883"]  
Thanks so much for the prompt reply!

My end goal is to filter out some of the key/value pairs that have been saved in context, and then shoot the smaller object out as a string via TCP.  
Thanks so much for the prompt reply!

My end goal is to filter out some of the key/value pairs that have been saved in context, and then shoot the smaller object out as a string via TCP.

Would you mind explaining the lines at the top that are commented out? I see some things (mqttWsPort) that I'm not familiar with. Still getting my footing with a lot of js 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 July 2019 21:09 UTC](https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883/5 "2019-07-03T21:09:40Z")

</div>

Sorry, I posted that rather quickly so didn't remove things specific to my config. The things you refer to are global variables that I set up in my `settings.js` file. I pass down some standard things like the IP address and port that I'm using. Not really needed.

The bit that is interesting is that you _can_, though generally _shouldn't_ access the globals via the built-in `context.global`. While this is generally frowned upon and may not work forever, it does work at the moment.

---

<div class="post-metadata">

**Author:** ![jovee](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jovee](https://discourse.nodered.org/u/jovee)\
**Post date:** [3 July 2019 21:20 UTC](https://discourse.nodered.org/t/get-everything-stored-in-global-context-as-object/12883/6 "2019-07-03T21:20:16Z")

</div>

No worries, thanks for clearing that up!

I'm still fumbling my way around creating a new object with the retrieved keys and values, but I'm sure it'll click sooner or later, as I continue to sharpen my googling.
