Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
Transform an explicit JSON payload into an array driven generic payload?

I am currently working in a project that has insisted in explicitly defining over 1,700 questions into a JSON data schema for an API and its getting out of control. I have been suggesting a more generic structure to the schema and let the data do the talking to tell you what the context is.

Whilst there are debates happening around which schema to use, we have decided on our internal systems, to go ahead and use a more generic model even if the externally facing model is the explicit one. This means we need an adapter to convert from one to the other, until such time as we can just use the one we wanted in the first place.

The business is a Java shop, I don't know whether to advise to build the adapter in Java or whether we can incorporate some lightweight JavaScript to do the work, maybe in the form of a configuration.

My question is: How would you approach converting the first JSON example into the second JSON example? It maps from explicitly defined objects to generic objects in arrays. Thanks for considering my question.

Example One

{
  "nested_object" : {
    "department_one" : {
      "floor" : "4",
      "product_one" : {
        "quantity" : 10,
        "size" : "L"
      },
      "product_two" : {
        "quantity" : 5,
        "size" : "S"
      }
    },
    "department_two" : {
      "floor" : "2",
      "product_thirteen" : {
        "quantity" : 1,
        "size" : "M"
      },
      "product_eleven" : {
        "quantity" : 8,
        "size" : "L"
      }
    }
  }
}

Example Two

{
  "departments" : [
    {
      "department_name" : "department_one",
      "floor" : "4",
      "products" : [
        {
          "product_name" : "product_one",
          "quantity" : 10,
          "size" : "L"
        },
        {
          "product_name" : "product_two",
          "quantity" : 5,
          "size" : "S"
        }
      ]
    },
    {
      "department_name" : "department_two",
      "floor" : "2",
      "products" : [
        {
          "product_name" : "product_thirteen",
          "quantity" : 1,
          "size" : "M"
        },
        {
          "product_name" : "product_eleven",
          "quantity" : 8,
          "size" : "L"
        }
      ]
    }
  ]
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could use a combination of Object.keys (to grab product and department names). Below is a quick implementation.

const obj1 = {
    "nested_object" : {
        "department_one" : {
            "floor" : "4",
            "product_one" : {
                "quantity" : 10,
                "size" : "L"
            },
            "product_two" : {
                "quantity" : 5,
                "size" : "S"
            }
        },
        "department_two" : {
            "floor" : "2",
            "product_thirteen" : {
                "quantity" : 1,
                "size" : "M"
            },
            "product_eleven" : {
                "quantity" : 8,
                "size" : "L"
            }
        }
    }
}

const transformedObj = {
  departments: [ ],
};
//holds all department names
const departmentKeys = Object.keys(obj1.nested_object)

const departmentsArr = departmentKeys.map((key) => {
    const floor = obj1.nested_object[key].floor
    //remove floor reference, since we already stored the value above
    delete obj1.nested_object[key].floor
    //holds all product names
    const productsKeysArr = Object.keys(obj1.nested_object[key])
    //holds all product objects for respective department
    const productsArr = productsKeysArr.map((product) => {
        const quantity = obj1.nested_object[key][product].quantity
        const size = obj1.nested_object[key][product].size
        return {
            product_name: product,
            quantity: quantity,
            size: size
        }
    })
    return {
        department_name: key, 
        floor: floor,
        products: productsArr
    }
})
//assign departments array to transformed object
transformedObj.departments = departmentsArr

console.log(transformedObj)

about 4 years ago · Juan Pablo Isaza Relatório

0

This would be my take on this. I like conciseness and expressiveness in implementations:

const data = { "nested_object": { ... }}

Object.entries(data.nested_object).map(([department_name, {floor, ...ps}]) => ({
  department_name,
  floor,
  products: Object.entries(ps).map(([product_name, p]) => ({product_name, ...p}))
})) 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda