Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
How to parse complex nested JSON data in NodeJS

I'm having kind of complex nested JSON in my dynamoDB, I want to extract this Filter object and its values. For example, if Type is macBook return the Filter object and its value PRO and AIR. Please find my code below and JSON input - I'm struggling to parse these value in NodeJS - could someone please help me - how to get the Filter Object?

JSON

{
  "ProductType": {
    "S": "Apple"
  },
  "Type": {
    "M": {
      "iPhone": {
        "M": {
          "Filter": {
            "M": {
              "model": {
                "SS": [
                  "X", "XS"
                ]
              }
            }
          }
        }
      },
      "macBook": {
        "M": {
          "Filter": {
            "M": {
              "model": {
                "SS": [
                  "PRO", "AIR"
                ]
              }
            }
          }
        }
      }
    }
  }
}

product.js

const AWS = require('aws-sdk');

var dynamodb = new AWS.DynamoDB({
    region: process.env.AWS_REGION,
  });

const getModel = function (productType) {
    return new Promise((resolve, reject) => {
        var params = {
            Key: {
                "ProductType": {
                    S: productType
                }
            },
            TableName: 'product'
        };
        dynamodb.getItem(params, function (err, data) {
            if (err) reject(err, err.stack);
            else {
                console.log("data: " + Object.keys(data));
                let product = data.Item.ProductType.S;
                let filterModel = data.Item.Type.M
                console.log(filterModel);

                
                
            }

        });
    });
}

getModel('Apple')

Any help would be much appreciated

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You need to use Object.values to get the values of iPhone and macbook objects, then inside you can use flatMap to find the Filter whose has PRO", "AIR" properties.

const data ={
  "ProductType": {
    "S": "Apple"
  },
  "Type": {
    "M": {
      "iPhone": {
        "M": {
          "Filter": {
            "M": {
              "model": {
                "SS": [
                  "X", "XS"
                ]
              }
            }
          }
        }
      },
      "macBook": {
        "M": {
          "Filter": {
            "M": {
              "model": {
                "SS": [
                  "PRO", "AIR"
                ]
              }
            }
          }
        }
      }
    }
  }
}


const result = Object.values(data.Type.M).flatMap((item) => {
  const SS = item.M.Filter.M.model.SS
  return SS.includes('PRO') && SS.includes('AIR') ? [{ Filter: item.M.Filter.M }] : []
}, [])

console.log(result)

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!