skip to Main Content

How can I convert JSON columns to JSON rows?

From:

[
  {
    "accountCode": "01",
    "accountName": "Cash on Hand",
    "HeadOffice": "100",
    "Marketing": "200",
    "Sales": "300",
    "Accounting": "400"
  },
  {
    "accountCode": "02",
    "accountName": "Cash in Bank",
    "HeadOffice": "150",
    "Marketing": "250",
    "Sales": "350",
    "Accounting": "450"
  },
  {
    "accountCode": "03",
    "accountName": "Loans",
    "HeadOffice": "250",
    "Marketing": "350",
    "Sales": "450",
    "Accounting": "450"
  },
  {
    "accountCode": "04",
    "accountName": "Advances",
    "HeadOffice": "150",
    "Marketing": "250",
    "Sales": "350",
    "Accounting": "450"
  }
]

To:

[
  {
    "AccountCode": "01",
    "accountName": "Cash on Hand",
    "costCenter": "HeadOffice",
    "amount": "100"
  },
  {
    "AccountCode": "01",
    "accountName": "Cash on Hand",
    "costCenter": "Marketing",
    "amount": "200"
  },
  {
    "AccountCode": "01",
    "accountName": "Cash on Hand",
    "costCenter": "Sales",
    "amount": "300"
  },
  {
    "AccountCode": "01",
    "accountName": "Cash on Hand",
    "costCenter": "Accounting",
    "amount": "400"
  },
  {
    "AccountCode": "02",
    "accountName": "Cash in Bank",
    "costCenter": "HeadOffice",
    "amount": "150"
  },
  {
    "AccountCode": "02",
    "accountName": "Cash in Bank",
    "costCenter": "Marketing",
    "amount": "250"
  },
  {
    "AccountCode": "02",
    "accountName": "Cash in Bank",
    "costCenter": "Sales",
    "amount": "350"
  },
  {
    "AccountCode": "02",
    "accountName": "Cash in Bank",
    "costCenter": "Accounting",
    "amount": "450"
  },
  {
    "AccountCode": "03",
    "accountName": "Loans",
    "costCenter": "HeadOffice",
    "amount": "250"
  },
  {
    "AccountCode": "03",
    "accountName": "Loans",
    "costCenter": "Marketing",
    "amount": "350"
  },
  {
    "AccountCode": "03",
    "accountName": "Loans",
    "costCenter": "Sales",
    "amount": "450"
  },
  {
    "AccountCode": "03",
    "accountName": "Loans",
    "costCenter": "Accounting",
    "amount": "450"
  },
  {
    "AccountCode": "04",
    "accountName": "Advances",
    "costCenter": "HeadOffice",
    "amount": "150"
  },
  {
    "AccountCode": "04",
    "accountName": "Advances",
    "costCenter": "Marketing",
    "amount": "250"
  },
  {
    "AccountCode": "04",
    "accountName": "Advances",
    "costCenter": "Sales",
    "amount": "350"
  },
  {
    "AccountCode": "04",
    "accountName": "Advances",
    "costCenter": "Accounting",
    "amount": "450"
  }
]

2

Answers


  1. To convert JSON columns to JSON rows, you can iterate over the original JSON array, extract the desired fields, and create a new JSON array with the desired structure.

    Example:

    # Iterate over the original JSON data
    for item in original_json:
        account_code = item["accountCode"]
        account_name = item["accountName"]
        
        # Iterate over the remaining key-value pairs in each object
        for key, value in item.items():
            if key not in ["accountCode", "accountName"]:
                result_json.append({
                    "AccountCode": account_code,
                    "accountName": account_name,
                    "costCenter": key,
                    "amount": value
                })
    
    # Serialize the result array to JSON
    result_json_str = json.dumps(result_json, indent=4)
    print(result_json_str)
    
    Login or Signup to reply.
  2. you can iterate over the initial JSON array and transform each object into multiple objects with the desired structure. Here’s a sample code snippet to achieve this:

    // Assuming your initial JSON data is stored in an array called jsonData
    let newData = [];
    
    jsonData.forEach(item => {
      Object.keys(item).forEach(key => {
        if (key !== 'accountCode' && key !== 'accountName') {
          newData.push({
            AccountCode: item.accountCode,
            accountName: item.accountName,
            costCenter: key,
            amount: item[key]
          });
        }
      });
    });
    
    // newData will now contain the transformed JSON rows
    console.log(newData);
    

    you should be able to convert the JSON columns to JSON rows as per your specified output structure in your Angular application

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search