Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda