Home > Software design >  How can i select a specific part of json data with nodejs?
How can i select a specific part of json data with nodejs?

Time:09-03

i have a nodeJS script that listens to for a webhook and then adds the data to firebase, the following works well:

        let data = {
                addresses: event.data["addresses"],
                code: event.data["code"],
                created_at: event.data["created_at"],
                exchange_rates: event.data["exchange_rates"],
                expires_at: event.data["expires_at"],
                pricing: event.data["pricing"]
        };

However, the pricing json contains a lot of junk and i would only like to add a specific part of the pricing json, below is an example of the json:

"pricing": {
    "theprice": "0000",
    "junk": "111",
    "junk1": 222,
    "junk2": 333
},

I would only like to select 'theprice' from the json. I have tried the following:

pricing: event.data["pricing"],["theprice"]

However this does not work, what is the correct syntax?

CodePudding user response:

Since your pricing is another object/map, you can get it with:

event.data["pricing"]["theprice"]

(so without the comma)

Alternatively, this also works:

event.data.pricing.theprice

CodePudding user response:

i don't know if this is a relatable solution but try parsing the data to json then try pricing: event.data.pricing.theprice .

  • Related