1

I have the JSON please see below and I need help to convert changing the format mentioned below. I want to move the data to different columns (format mentioned below)

JSON I have

const data =
[
    {
        "2":{
            "value":1.2
        },
        "3":{
            "values":{
                "10.0":1.3
            }
        },
        "4":{
            "value":1.6
        },
        "key":"abc",
        "count":2
    },
    {
        "2":{
            "value":2.2
        },
        "3":{
            "values":{
                "10.0":1.2
            }
        },
        "4":{
            "value":5.6
        },
        "key":"xyz",
        "count":22
    },
    
]

I want to change the above to the below format:

Expected format

const data =
[
    {
        col1: "1.2",
        col2: "1.3",
        col3: "1.6",
        col4: "abc",
        col5: "2"
    },
    {
        col1: "2.2",
        col2: "1.2",
        col3: "5.6",
        col4: "xyz",
        col5: "22"
    },
    
]

2 Answers 2

1

Here's a solution that flattens arbitrarily nested json inputs, assuming only that each column has only one primitive value. It takes advantage of the fact that arrays in js are really just objects with numeric keys.

const data = [
  {
    '2': { value: 1.2, },
    '3': { values: { '10.0': 1.3, }, },
    '4': { value: 1.6, },
  },
  {
    '2': { value: 2.2, },
    '3': { values: { '10.0': 1.2, }, },
    '4': { value: 5.6, },
  },
];

const flattenedValue = value => {
  while (typeof value == 'object' && value != null) 
    for (const v of Object.values(value)) 
      value = v; 
  return value;
};
const result = data.map(row => 
  Object.fromEntries(
    Object.values(row).map(flattenedValue).map((v, i) => [`col${i + 1}`, v])
  )
);

console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the solution just a quick question if I want to add some logic for example dividing the value of the col3 with col1 and saving them as col3 how can i do it within your logic?
It's more trouble than it's worth to try to do it inside the method, since the whole point is it's generalized. You should just manually iterate through the object afterwards, e.g. for (row of result) row.col3 = row.col3/ row.col1;. You could also do the modification inside the row => arrow function, but you'd have to wrap the statement in { }, save the object to a variable, and add an explicit return statement.
1

I believe this is what you are looking for.

I assumed there are just value or values properties and in values you only need the first one. If my assumption is correct here is your snippet below.

const data = [
  {
    '2': {
      value: 1.2,
    },
    '3': {
      values: {
        '10.0': 1.3,
      },
    },
    '4': {
      value: 1.6,
    },
  },
  {
    '2': {
      value: 2.2,
    },
    '3': {
      values: {
        '10.0': 1.2,
      },
    },
    '4': {
      value: 5.6,
    },
  },
];

const result = data.map((item) => {
  return Object.values(item)
    .reduce((acc, { value = null, values = null }, index) => {
      if (value) return { ...acc, [`col${index + 1}`]: String(value) };
      if (values) {
        const [firstItem = null] = Object.values(values);
        if (firstItem) return { ...acc, [`col${index + 1}`]: String(firstItem) };
      }
    }, {});
});

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.