Home > database >  Can we organize the Json Data based on one field?
Can we organize the Json Data based on one field?

Time:07-12

I have a common field in each JSON object, i.e Field A. And I have Variable fields depending on data from service, i.e,(Variable Field A and Variable Field B).

I have a JSON data in this below format:

[{
    "Field A": "ABC",
    "Variable Field A": "66"
},
{
    "Field A": "DEF",
    "Variable Field A": "70"
},
{
    "Field A": "GHI",
    "Variable Field A": "135"
},
{
    "Field A": "JKL",
    "Variable Field A": "19"
},
{
    "Field A": "ABC",
    "Variable Field B": "-729"
},
{
    "Field A": "GHI",
    "Variable Field B": "962"
},
{
    "Field A": "DEF",
    "Variable Field B": "334"
},
{
    "Field A": "JKL",
    "Variable Field B":"241"
}]

I need to put the variable fields together based on common field (here Field A), so that all variable fields for one common field is available within one object. How can I make my JSON converted to something like this?

[{
    "Field A": "ABC",
    "Variable Field A": "66",
    "Variable Field B": "-729"
},
{
    "Field A": "DEF",
    "Variable Field A": "70",
    "Variable Field B": "334"
},
{
    "Field A": "GHI",
    "Variable Field A": "135",
    "Variable Field B": "962"
},
{
    "Field A": "JKL",
    "Variable Field A": "19",
    "Variable Field B": "241
}]

CodePudding user response:

Decode your json in the the proper datastructure and recreate your array of objects using the proper algorithm for the matching.

Something like:

  1. take the first object
  2. save the Field A
  3. iterate other objects and check for the field A
  4. if you find another Field A add the new "Variable Field B" in the new object structure with the found value or the new "Variable Field A" with the found value.
  5. delete the found items in the original array (in order to prevent duplications)
  6. next iteration

If you're using javascript for that check .hasOwnProperty("propertyName") method that check if a field exists in the object. It should be usefull for checking if you have to get "Variable Field A" or "Variable Field B" from the object.

CodePudding user response:

This is a great application for the Array reducer function. Try this:

const data = [] // your JSON data
data.reduce((acc,item)=>({
  ...acc,
  [item['Field A']]: {...acc[item['Field A']],...item}
}),{})

The result:

{
  ABC: {
    'Field A': 'ABC',
    'Variable Field A': '66',
    'Variable Field B': '-729'
  },
  DEF: {
    'Field A': 'DEF',
    'Variable Field A': '70',
    'Variable Field B': '334'
  },
  GHI: {
    'Field A': 'GHI',
    'Variable Field A': '135',
    'Variable Field B': '962'
  },
  JKL: {
    'Field A': 'JKL',
    'Variable Field A': '19',
    'Variable Field B': '241'
  }
}

CodePudding user response:

you can do something like this

const group = (data, field) => Object.values(data.reduce((res, item) => {
  const existing = res[item[field]] || {}
  
  return {
    ...res,
    [item[field]]: {...existing, ...item}
  }

}, {}))


const data = [{
    "Field A": "ABC",
    "Variable Field A": "66"
},
{
    "Field A": "DEF",
    "Variable Field A": "70"
},
{
    "Field A": "GHI",
    "Variable Field A": "135"
},
{
    "Field A": "JKL",
    "Variable Field A": "19"
},
{
    "Field A": "ABC",
    "Variable Field B": "-729"
},
{
    "Field A": "GHI",
    "Variable Field B": "962"
},
{
    "Field A": "DEF",
    "Variable Field B": "334"
},
{
    "Field A": "JKL",
    "Variable Field B":"241"
}]

const result = group(data, 'Field A')

console.log(result)

CodePudding user response:

I copy pasted and modified an answer from this website. Very cryptic but it first creates an object grouped by "Field A" then flatten it to an array.

var arr = get_data();

var result = (Object.values(arr.reduce(function(r, a) {
  var A = a["Field A"]
  r[A] = r[A] || {};
  for (var key in a) {
    r[A][key] = a[key]
  }
  return r;
}, {})).flat())

console.log(result);


function get_data() {
  return [{
      "Field A": "ABC",
      "Variable Field A": "66"
    },
    {
      "Field A": "DEF",
      "Variable Field A": "70"
    },
    {
      "Field A": "GHI",
      "Variable Field A": "135"
    },
    {
      "Field A": "JKL",
      "Variable Field A": "19"
    },
    {
      "Field A": "ABC",
      "Variable Field B": "-729"
    },
    {
      "Field A": "GHI",
      "Variable Field B": "962"
    },
    {
      "Field A": "DEF",
      "Variable Field B": "334"
    },
    {
      "Field A": "JKL",
      "Variable Field B": "241"
    }
  ];
}
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

  • Related