Home > Enterprise >  Transform an explicit JSON payload into an array driven generic payload?
Transform an explicit JSON payload into an array driven generic payload?

Time:04-20

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"
        }
      ]
    }
  ]
}

CodePudding user response:

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)

CodePudding user response:

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}))
})) 
  • Related