将JSON转换为CSV并展开嵌套的JSON

15
我试图将JSON数据扁平化,以便将其解析为CSV格式。但是扁平化并不完全正确。当我尝试将要扁平化的JSON数据中的customer.addresses字段进行扁平化处理时,它会填充addresstype:r,然后跳过所有其他字段(如city、countrycode、countycode等),然后从customer.companyName开始。嵌套的JSON无法正确地分解以在Excel中显示。我认为我的JavaScript代码可能有点问题,希望能得到任何帮助。

JSON (这是嵌套JSON数据的一部分,它不总是在相同的层级中,是否有一种编码方式可以读取所有层级的任何类型的嵌套JSON数据)

[
  {
    "countyCode": 12,
    "customer": {
      "addresses": [
        {
          "addressType": "R",
          "city": "BRADENTON",
          "countryCode": "US",
          "countyCode": 12,
          "foreignPostalCode": null,
          "state": "FL",
          "streetAddress": "819 15th Ave Dr E",
          "zipCode": 34211,
          "zipPlus": null
        },
        {
          "addressType": "M",
          "city": "BRADENTON",
          "countryCode": "US",
          "countyCode": 12,
          "foreignPostalCode": null,
          "state": "FL",
          "streetAddress": "PO BOX 124",
          "zipCode": 34201,
          "zipPlus": 0124
        }
      ],
      "companyName": null,
      "customerNumber": 932874,
      "customerStopFlag": false,
      "customerType": "I",
      "dateOfBirth": "1936-08-05T00:00:00",
      "dlExpirationDate": "2022-08-05T00:00:00",
      "dlRenewalEligibilityFlag": true,
      "driverLicenseNumber": "B360722339284",
      "emailAddress": null,
      "feidNumber": null,
      "firstName": "David",
      "lastName": "Brierton",
      "middleName": "Hugh",
      "militaryExemptionFlag": null,
      "nameSuffix": null,
      "sex": "M"

JS

JavaScript (JS)
function flatObjectToString(obj) {
    var s = "";
    Object.keys(obj).map(key => {
      if (obj[key] === null) {
        s += key + ":";
      } else if (obj[key].toLocaleDateString) {
        s += key + ": " + obj[key].toLocaleDateString() + "\n";
      } else if (obj[key] instanceof Array) {
        s += key + ":\n" + listToFlatString(obj[key]);
      } else if (typeof obj[key] == "object") {
        s += key + ":\n" + flatObjectToString(obj[key]);
      } else {
        s += key + ":" + obj[key];
      }
      s += "\n";
    });
    return s;
  }

  function listToFlatString(list) {
    var s = "";
    list.map(item => {
      Object.keys(item).map(key => {
        s += "";
        if (item[key] instanceof Array) {
          s += key + "\n" + listToFlatString(item[key]);
        } else if (typeof item[key] == "object" && item[key] !== null) {
          s += key + ": " + flatObjectToString(item[key]);
        } else {
          s += key + ": " + (item[key] === null ? "" : item[key].toLocaleDateString ? item[key].toLocaleDateString : item[key].toString());
        }
        s += "\n";
      });
    });
    return s;
  }

  function flatten(object, addToList, prefix) {
    Object.keys(object).map(key => {
      if (object[key] === null) {
        addToList[prefix + key] = "";
      } else
      if (object[key] instanceof Array) {
        addToList[prefix + key] = listToFlatString(object[key]);
      } else if (typeof object[key] == 'object' && !object[key].toLocaleDateString) {
        flatten(object[key], addToList, prefix + key + '.');
      } else {
        addToList[prefix + key] = object[key];
      }
    });
    return addToList;
  }

然后我将它通过 Javascript 工具运行,使用以下代码:

// Run the JSON string through the flattening utilities above
          var flatJSON = JSON.parse(evt.target.result).map(record => flatten(record, {}, ''));

          var csv = Papa.unparse(flatJSON);

1
你的期望输出是什么? - Matt Burland
有多个地址,因此我们想知道输出应该是什么样子(所有地址在一列中,还是每个地址占据一行)。 - Kai
@DavidBrierton,10GB?它是一个单独的JSON文件还是多个嵌入到单个文件中的JSON? - Tarun Lalwani
让我们在聊天中继续这个讨论 - Tarun Lalwani
@DavidBrierton,赏金即将到期,您的修复代码已经发布为答案。别忘了在它到期之前接受其中一个答案并颁发赏金。 - Tarun Lalwani
显示剩余2条评论
6个回答

9
如果您正在寻找修复实际代码的方法,则需要使用以下类似的内容。
function flatten(object, addToList, prefix) {
    Object.keys(object).map(key => {
        if (object[key] === null) {
            addToList[prefix + key] = "";
        } else
        if (object[key] instanceof Array) {
            // addToList[prefix + key] = listToFlatString(object[key]);
            for (i in object[key]) {
                flatten(object[key][i], addToList, prefix + key + "." + i)
            }
        } else if (typeof object[key] == 'object' && !object[key].toLocaleDateString) {
            flatten(object[key], addToList, prefix + key + '.');
        } else {
            addToList[prefix + key] = object[key];
        }
    });
    return addToList;
}

我替换

addToList[prefix + key] = listToFlatString(object[key]);

使用

for (i in object[key]) {
   flatten(object[key][i], addToList, prefix + key + "." + i)
}

那么问题就解决了,以下是根据您提供的示例JSON输出的结果。
[ '0.countyCode': 12,
  '0.excludedFlag': '',
  '0.fees.annualTotalFees': 35.6,
  '0.fees.annualTotalFeesMail': 36.35,
  '0.fees.annualTotalFeesOnline': 38.35,
  '0.fees.biennialTotalFees': 71.2,
  '0.fees.biennialTotalFeesMail': 71.95,
  '0.fees.biennialTotalFeesOnline': 73.95,
  '0.fees.branchFeeFlag': false,
  '0.fees.delinquentFeeAmount': 5,
  '0.fees.mhBackTax': 0,
  '0.fees.mhBackTaxMonths': 0,
  '0.fileId': 522,
  '0.id': '0002be04-546-4a92-a3d7-c31546544f',
  '0.messages.0messageCode': 'RN2',
  '0.messages.0messageText': 'Plate must be replaced if a 2 year renewal is desired.',
  '0.messages.0messageType': 'I',
  '0.messages.1messageCode': 'RN40',
  '0.messages.1messageText': 'You must complete the affidavit on the reverse side or provide a copy of your Florida insurance identification card.',
  '0.messages.1messageType': 'I',
  '0.messages.2messageCode': 'RN41',
  '0.messages.2messageText': 'Insurance is on file.',
  '0.messages.2messageType': 'II',
  '0.registrationDetail.annualPlateReplacementFlag': false,
  '0.registrationDetail.arfCredit': 25.6,
  '0.registrationDetail.biennialPlateReplacementFlag': true,
  '0.registrationDetail.customerStopFlag': '',
  '0.registrationDetail.delinquentDate': '2018-02-11T00:00:00',
  '0.registrationDetail.expirationDate': '2018-01-09T00:00:00',
  '0.registrationDetail.foreignAddressFlag': false,
  '0.registrationDetail.honorayConsulPlateFlag': '',
  '0.registrationDetail.hovDecalNumber': '',
  '0.registrationDetail.hovDecalYear': '',
  '0.registrationDetail.hovExpirationDate': '',
  '0.registrationDetail.inventorySubtype': 'RP',
  '0.registrationDetail.lastActivityCounty': 12,
  '0.registrationDetail.legislativePlateDueForReplacementFlag': false,
  '0.registrationDetail.licensePlateCode': 'RGR',
  '0.registrationDetail.licensePlateNumber': 'L45656',
  '0.registrationDetail.mailToAddressFlag': false,
  '0.registrationDetail.mailToCustomerNumber': '',
  '0.registrationDetail.mhLocationCode': '',
  '0.registrationDetail.militaryOwnerFlag': false,
  '0.registrationDetail.numberOfRegistrants': 1,
  '0.registrationDetail.onlineRenewalEligibilityFlag': true,
  '0.registrationDetail.pinNumber': 222354654,
  '0.registrationDetail.plateExpirationDate': '2018-03-18T00:00:00',
  '0.registrationDetail.plateIssueDate': '2008-09-18T00:00:00',
  '0.registrationDetail.possibleNgExemptionFlag': false,
  '0.registrationDetail.registrationNumber': 2234545345,
  '0.registrationDetail.registrationOnlyFlag': '',
  '0.registrationDetail.registrationType': 'R',
  '0.registrationDetail.registrationUse': 'PR',
  '0.registrationDetail.renewalCountyCode': 12,
  '0.registrationDetail.rentalParkFlag': '',
  '0.registrationDetail.seminoleMiccosukeeIndianFlag': false,
  '0.registrationDetail.taxCollectorRenewalEligibilityFlag': true,
  '0.registrationDetail.vehicleClassCode': 1,
  '0.registrationOwners.0customer.addresses.0addressType': 'R',
  '0.registrationOwners.0customer.addresses.0city': 'PALMETTO',
  '0.registrationOwners.0customer.addresses.0countryCode': 'US',
  '0.registrationOwners.0customer.addresses.0countyCode': 12,
  '0.registrationOwners.0customer.addresses.0foreignPostalCode': '',
  '0.registrationOwners.0customer.addresses.0state': 'FL',
  '0.registrationOwners.0customer.addresses.0streetAddress': '34545 7TH AVE W',
  '0.registrationOwners.0customer.addresses.0zipCode': 34221,
  '0.registrationOwners.0customer.addresses.0zipPlus': '',
  '0.registrationOwners.0customer.addresses.1addressType': 'M',
  '0.registrationOwners.0customer.addresses.1city': 'PALMETTO',
  '0.registrationOwners.0customer.addresses.1countryCode': 'US',
  '0.registrationOwners.0customer.addresses.1countyCode': 12,
  '0.registrationOwners.0customer.addresses.1foreignPostalCode': '',
  '0.registrationOwners.0customer.addresses.1state': 'FL',
  '0.registrationOwners.0customer.addresses.1streetAddress': '34545 7TH AVE W',
  '0.registrationOwners.0customer.addresses.1zipCode': 34221,
  '0.registrationOwners.0customer.addresses.1zipPlus': 3433,
  '0.registrationOwners.0customer.companyName': '',
  '0.registrationOwners.0customer.customerNumber': 3346645,
  '0.registrationOwners.0customer.customerStopFlag': false,
  '0.registrationOwners.0customer.customerType': 'I',
  '0.registrationOwners.0customer.dateOfBirth': '1971-01-09T00:00:00',
  '0.registrationOwners.0customer.dlExpirationDate': '2025-01-09T00:00:00',
  '0.registrationOwners.0customer.dlRenewalEligibilityFlag': true,
  '0.registrationOwners.0customer.driverLicenseNumber': 'F34545345345',
  '0.registrationOwners.0customer.emailAddress': '',
  '0.registrationOwners.0customer.feidNumber': '',
  '0.registrationOwners.0customer.firstName': 'DAVID',
  '0.registrationOwners.0customer.lastName': 'HUGH',
  '0.registrationOwners.0customer.middleName': 'BRIERTON',
  '0.registrationOwners.0customer.militaryExemptionFlag': false,
  '0.registrationOwners.0customer.nameSuffix': '',
  '0.registrationOwners.0customer.sex': 'F',
  '0.registrationOwners.0registrationOwnershipNumber': 1,
  '0.shippingAddress.address.addressType': 'S',
  '0.shippingAddress.address.city': 'PALMETTO',
  '0.shippingAddress.address.countryCode': 'US',
  '0.shippingAddress.address.countyCode': 12,
  '0.shippingAddress.address.foreignPostalCode': '',
  '0.shippingAddress.address.state': 'FL',
  '0.shippingAddress.address.streetAddress': '34545 7TH AVE W',
  '0.shippingAddress.address.zipCode': 34221,
  '0.shippingAddress.address.zipPlus': 8344,
  '0.shippingAddress.shippingCompanyName': '',
  '0.shippingAddress.shippingName1': 'DAVID HUGH BRIERTON',
  '0.shippingAddress.shippingName2': '',
  '0.stops': '',
  '0.vehicle.address': '',
  '0.vehicle.bodyCode': '4D',
  '0.vehicle.brakeHorsePower': '',
  '0.vehicle.cubicCentimeters': '',
  '0.vehicle.grossWeight': '',
  '0.vehicle.insuranceAffidavitCode': '',
  '0.vehicle.leaseOwnerFlag': false,
  '0.vehicle.lengthFeet': '',
  '0.vehicle.lengthInches': '',
  '0.vehicle.majorColorCode': 'GLD',
  '0.vehicle.makeCode': 'CHEV',
  '0.vehicle.minorColorCode': '',
  '0.vehicle.netWeight': 3200,
  '0.vehicle.numberOfAxles': '',
  '0.vehicle.ownerUnitNumber': '',
  '0.vehicle.titleNumber': 345454534,
  '0.vehicle.vehicleIdentificationNumber': '1G1ZD345U5B345435',
  '0.vehicle.vehicleNumber': 23454656,
  '0.vehicle.vehicleType': 'AU',
  '0.vehicle.vesselCode': '',
  '0.vehicle.vesselManufacturerDesc': '',
  '0.vehicle.vesselResidentStatus': 'Y',
  '0.vehicle.vesselWaterType': '',
  '0.vehicle.widthFeet': '',
  '0.vehicle.widthInches': '',
  '0.vehicle.yearMake': 2011,
  '1.countyCode': 12,
  '1.excludedFlag': '',
  '1.fees.annualTotalFees': 27.6,
  '1.fees.annualTotalFeesMail': 28.35,
  '1.fees.annualTotalFeesOnline': 30.35,
  '1.fees.biennialTotalFees': 55.2,
  '1.fees.biennialTotalFeesMail': 55.95,
  '1.fees.biennialTotalFeesOnline': 57.95,
  '1.fees.branchFeeFlag': false,
  '1.fees.delinquentFeeAmount': 5,
  '1.fees.mhBackTax': 0,
  '1.fees.mhBackTaxMonths': 0,
  '1.fileId': 522,
  '1.id': '0008c654-8960-45b8-b416-cff3456767',
  '1.messages.0messageCode': 'RN40',
  '1.messages.0messageText': 'You must complete the affidavit on the reverse side or provide a copy of your Florida insurance identification card.',
  '1.messages.0messageType': 'I',
  '1.registrationDetail.annualPlateReplacementFlag': false,
  '1.registrationDetail.arfCredit': 2.8,
  '1.registrationDetail.biennialPlateReplacementFlag': false,
  '1.registrationDetail.customerStopFlag': '',
  '1.registrationDetail.delinquentDate': '2018-02-11T00:00:00',
  '1.registrationDetail.expirationDate': '2018-01-01T00:00:00',
  '1.registrationDetail.foreignAddressFlag': false,
  '1.registrationDetail.honorayConsulPlateFlag': '',
  '1.registrationDetail.hovDecalNumber': '',
  '1.registrationDetail.hovDecalYear': '',
  '1.registrationDetail.hovExpirationDate': '',
  '1.registrationDetail.inventorySubtype': 'SS',
  '1.registrationDetail.lastActivityCounty': 16,
  '1.registrationDetail.legislativePlateDueForReplacementFlag': false,
  '1.registrationDetail.licensePlateCode': 'RGS',
  '1.registrationDetail.licensePlateNumber': 'HU34598',
  '1.registrationDetail.mailToAddressFlag': false,
  '1.registrationDetail.mailToCustomerNumber': '',
  '1.registrationDetail.mhLocationCode': '',
  '1.registrationDetail.militaryOwnerFlag': false,
  '1.registrationDetail.numberOfRegistrants': 1,
  '1.registrationDetail.onlineRenewalEligibilityFlag': true,
  '1.registrationDetail.pinNumber': 4936856,
  '1.registrationDetail.plateExpirationDate': '2026-09-24T00:00:00',
  '1.registrationDetail.plateIssueDate': '2017-03-24T00:00:00',
  '1.registrationDetail.possibleNgExemptionFlag': false,
  '1.registrationDetail.registrationNumber': 4095685,
  '1.registrationDetail.registrationOnlyFlag': '',
  '1.registrationDetail.registrationType': 'R',
  '1.registrationDetail.registrationUse': 'PR',
  '1.registrationDetail.renewalCountyCode': 12,
  '1.registrationDetail.rentalParkFlag': '',
  '1.registrationDetail.seminoleMiccosukeeIndianFlag': false,
  '1.registrationDetail.taxCollectorRenewalEligibilityFlag': true,
  '1.registrationDetail.vehicleClassCode': 1,
  '1.registrationOwners.0customer.addresses.0addressType': 'R',
  '1.registrationOwners.0customer.addresses.0city': 'SARASOTA',
  '1.registrationOwners.0customer.addresses.0countryCode': 'US',
  '1.registrationOwners.0customer.addresses.0countyCode': 12,
  '1.registrationOwners.0customer.addresses.0foreignPostalCode': '',
  '1.registrationOwners.0customer.addresses.0state': 'FL',
  '1.registrationOwners.0customer.addresses.0streetAddress': '5858 FRUITVILLE RD',
  '1.registrationOwners.0customer.addresses.0zipCode': 34240,
  '1.registrationOwners.0customer.addresses.0zipPlus': 5858,
  '1.registrationOwners.0customer.addresses.1addressType': 'M',
  '1.registrationOwners.0customer.addresses.1city': 'SARASOTA',
  '1.registrationOwners.0customer.addresses.1countryCode': 'US',
  '1.registrationOwners.0customer.addresses.1countyCode': 16,
  '1.registrationOwners.0customer.addresses.1foreignPostalCode': '',
  '1.registrationOwners.0customer.addresses.1state': 'FL',
  '1.registrationOwners.0customer.addresses.1streetAddress': '5858 FRUITVILLE RD',
  '1.registrationOwners.0customer.addresses.1zipCode': 34240,
  '1.registrationOwners.0customer.addresses.1zipPlus': 5858,
  '1.registrationOwners.0customer.companyName': '',
  '1.registrationOwners.0customer.customerNumber': 2928357,
  '1.registrationOwners.0customer.customerStopFlag': false,
  '1.registrationOwners.0customer.customerType': 'I',
  '1.registrationOwners.0customer.dateOfBirth': '1989-01-01T00:00:00',
  '1.registrationOwners.0customer.dlExpirationDate': '2022-01-01T00:00:00',
  '1.registrationOwners.0customer.dlRenewalEligibilityFlag': true,
  '1.registrationOwners.0customer.driverLicenseNumber': 'B94832734',
  '1.registrationOwners.0customer.emailAddress': '',
  '1.registrationOwners.0customer.feidNumber': '',
  '1.registrationOwners.0customer.firstName': 'DAVID1',
  '1.registrationOwners.0customer.lastName': 'HUGH1',
  '1.registrationOwners.0customer.middleName': 'BRIERTON1',
  '1.registrationOwners.0customer.militaryExemptionFlag': false,
  '1.registrationOwners.0customer.nameSuffix': '',
  '1.registrationOwners.0customer.sex': 'M',
  '1.registrationOwners.0registrationOwnershipNumber': 1,
  '1.shippingAddress.address.addressType': 'S',
  '1.shippingAddress.address.city': 'SARASOTA',
  '1.shippingAddress.address.countryCode': 'US',
  '1.shippingAddress.address.countyCode': 16,
  '1.shippingAddress.address.foreignPostalCode': '',
  '1.shippingAddress.address.state': 'FL',
  '1.shippingAddress.address.streetAddress': '5858 FRUITVILLE RD',
  '1.shippingAddress.address.zipCode': 34240,
  '1.shippingAddress.address.zipPlus': 5858,
  '1.shippingAddress.shippingCompanyName': '',
  '1.shippingAddress.shippingName1': 'DAVID1 HUGH1 BRIERTON1',
  '1.shippingAddress.shippingName2': '',
  '1.stops': '',
  '1.vehicle.address': '',
  '1.vehicle.bodyCode': '4D',
  '1.vehicle.brakeHorsePower': '',
  '1.vehicle.cubicCentimeters': '',
  '1.vehicle.grossWeight': '',
  '1.vehicle.insuranceAffidavitCode': '',
  '1.vehicle.leaseOwnerFlag': false,
  '1.vehicle.lengthFeet': '',
  '1.vehicle.lengthInches': '',
  '1.vehicle.majorColorCode': 'BLU',
  '1.vehicle.makeCode': 'STRN',
  '1.vehicle.minorColorCode': '',
  '1.vehicle.netWeight': 2290,
  '1.vehicle.numberOfAxles': '',
  '1.vehicle.ownerUnitNumber': '',
  '1.vehicle.titleNumber': 239874,
  '1.vehicle.vehicleIdentificationNumber': '1G832492871Z23094',
  '1.vehicle.vehicleNumber': 239084,
  '1.vehicle.vehicleType': 'AU',
  '1.vehicle.vesselCode': '',
  '1.vehicle.vesselManufacturerDesc': '',
  '1.vehicle.vesselResidentStatus': 'Y',
  '1.vehicle.vesselWaterType': '',
  '1.vehicle.widthFeet': '',
  '1.vehicle.widthInches': '',
  '1.vehicle.yearMake': 2001,
  '2.countyCode': 12,
  '2.excludedFlag': '',
  '2.fees.annualTotalFees': 45.6,
  '2.fees.annualTotalFeesMail': 46.35,
  '2.fees.annualTotalFeesOnline': 48.35,
  '2.fees.biennialTotalFees': 91.2,
  '2.fees.biennialTotalFeesMail': 91.95,
  '2.fees.biennialTotalFeesOnline': 93.95,
  '2.fees.branchFeeFlag': false,
  '2.fees.delinquentFeeAmount': 10,
  '2.fees.mhBackTax': 0,
  '2.fees.mhBackTaxMonths': 0,
  '2.fileId': 522,
  '2.id': '000e3450d-3454-499a-ae70-de5676577',
  '2.messages.0messageCode': 'RN40',
  '2.messages.0messageText': 'You must complete the affidavit on the reverse side or provide a copy of your Florida insurance identification card.',
  '2.messages.0messageType': 'I',
  '2.registrationDetail.annualPlateReplacementFlag': false,
  '2.registrationDetail.arfCredit': 8.4,
  '2.registrationDetail.biennialPlateReplacementFlag': false,
  '2.registrationDetail.customerStopFlag': '',
  '2.registrationDetail.delinquentDate': '2018-02-11T00:00:00',
  '2.registrationDetail.expirationDate': '2018-01-11T00:00:00',
  '2.registrationDetail.foreignAddressFlag': false,
  '2.registrationDetail.honorayConsulPlateFlag': '',
  '2.registrationDetail.hovDecalNumber': '',
  '2.registrationDetail.hovDecalYear': '',
  '2.registrationDetail.hovExpirationDate': '',
  '2.registrationDetail.inventorySubtype': 'RP',
  '2.registrationDetail.lastActivityCounty': 12,
  '2.registrationDetail.legislativePlateDueForReplacementFlag': false,
  '2.registrationDetail.licensePlateCode': 'RGR',
  '2.registrationDetail.licensePlateNumber': '808IUT',
  '2.registrationDetail.mailToAddressFlag': false,
  '2.registrationDetail.mailToCustomerNumber': '',
  '2.registrationDetail.mhLocationCode': '',
  '2.registrationDetail.militaryOwnerFlag': false,
  '2.registrationDetail.numberOfRegistrants': 1,
  '2.registrationDetail.onlineRenewalEligibilityFlag': true,
  '2.registrationDetail.pinNumber': 934597,
  '2.registrationDetail.plateExpirationDate': '2023-06-06T00:00:00',
  '2.registrationDetail.plateIssueDate': '2013-12-06T00:00:00',
  '2.registrationDetail.possibleNgExemptionFlag': false,
  '2.registrationDetail.registrationNumber': 39287432,
  '2.registrationDetail.registrationOnlyFlag': '',
  '2.registrationDetail.registrationType': 'R',
  '2.registrationDetail.registrationUse': 'PR',
  '2.registrationDetail.renewalCountyCode': 12,
  '2.registrationDetail.rentalParkFlag': '',
  '2.registrationDetail.seminoleMiccosukeeIndianFlag': false,
  '2.registrationDetail.taxCollectorRenewalEligibilityFlag': true,
  '2.registrationDetail.vehicleClassCode': 1,
  '2.registrationOwners.0customer.addresses.0addressType': 'R',
  '2.registrationOwners.0customer.addresses.0city': 'SARASOTA',
  '2.registrationOwners.0customer.addresses.0countryCode': 'US',
  '2.registrationOwners.0customer.addresses.0countyCode': 12,
  '2.registrationOwners.0customer.addresses.0foreignPostalCode': '',
  '2.registrationOwners.0customer.addresses.0state': 'FL',
  '2.registrationOwners.0customer.addresses.0streetAddress': '39875 44TH DR E',
  '2.registrationOwners.0customer.addresses.0zipCode': 34243,
  '2.registrationOwners.0customer.addresses.0zipPlus': 5566,
  '2.registrationOwners.0customer.addresses.1addressType': 'M',
  '2.registrationOwners.0customer.addresses.1city': 'PALMETTO',
  '2.registrationOwners.0customer.addresses.1countryCode': 'US',
  '2.registrationOwners.0customer.addresses.1countyCode': 12,
  '2.registrationOwners.0customer.addresses.1foreignPostalCode': '',
  '2.registrationOwners.0customer.addresses.1state': 'FL',
  '2.registrationOwners.0customer.addresses.1streetAddress': '39875 44TH DR E',
  '2.registrationOwners.0customer.addresses.1zipCode': 34221,
  '2.registrationOwners.0customer.addresses.1zipPlus': '',
  '2.registrationOwners.0customer.companyName': '',
  '2.registrationOwners.0customer.customerNumber': 2398574,
  '2.registrationOwners.0customer.customerStopFlag': false,
  '2.registrationOwners.0customer.customerType': 'I',
  '2.registrationOwners.0customer.dateOfBirth': '1958-01-11T00:00:00',
  '2.registrationOwners.0customer.dlExpirationDate': '2020-01-11T00:00:00',
  '2.registrationOwners.0customer.dlRenewalEligibilityFlag': true,
  '2.registrationOwners.0customer.driverLicenseNumber': 'B23987433',
  '2.registrationOwners.0customer.emailAddress': '',
  '2.registrationOwners.0customer.feidNumber': '',
  '2.registrationOwners.0customer.firstName': 'DAVID2',
  '2.registrationOwners.0customer.lastName': 'HUGH2',
  '2.registrationOwners.0customer.middleName': 'BRIERTON2',
  '2.registrationOwners.0customer.militaryExemptionFlag': false,
  '2.registrationOwners.0customer.nameSuffix': '',
  '2.registrationOwners.0customer.sex': 'M',
  '2.registrationOwners.0registrationOwnershipNumber': 1,
  '2.shippingAddress.address.addressType': 'S',
  '2.shippingAddress.address.city': 'PALMETTO',
  '2.shippingAddress.address.countryCode': 'US',
  '2.shippingAddress.address.countyCode': 12,
  '2.shippingAddress.address.foreignPostalCode': '',
  '2.shippingAddress.address.state': 'FL',
  '2.shippingAddress.address.streetAddress': '293847 33TH ST W',
  '2.shippingAddress.address.zipCode': 34221,
  '2.shippingAddress.address.zipPlus': '',
  '2.shippingAddress.shippingCompanyName': '',
  '2.shippingAddress.shippingName1': 'DAVID2 HUGH2 BRIERTON2',
  '2.shippingAddress.shippingName2': '',
  '2.stops': '',
  '2.vehicle.address': '',
  '2.vehicle.bodyCode': '2D',
  '2.vehicle.brakeHorsePower': '',
  '2.vehicle.cubicCentimeters': '',
  '2.vehicle.grossWeight': '',
  '2.vehicle.insuranceAffidavitCode': '',
  '2.vehicle.leaseOwnerFlag': false,
  '2.vehicle.lengthFeet': '',
  '2.vehicle.lengthInches': '',
  '2.vehicle.majorColorCode': 'BLK',
  '2.vehicle.makeCode': 'PONT',
  '2.vehicle.minorColorCode': '',
  '2.vehicle.netWeight': 3802,
  '2.vehicle.numberOfAxles': '',
  '2.vehicle.ownerUnitNumber': '',
  '2.vehicle.titleNumber': 239857424,
  '2.vehicle.vehicleIdentificationNumber': '6G23242312UX63297437',
  '2.vehicle.vehicleNumber': '35T7843',
  '2.vehicle.vehicleType': 'AU',
  '2.vehicle.vesselCode': '',
  '2.vehicle.vesselManufacturerDesc': '',
  '2.vehicle.vesselResidentStatus': 'Y',
  '2.vehicle.vesselWaterType': '',
  '2.vehicle.widthFeet': '',
  '2.vehicle.widthInches': '',
  '2.vehicle.yearMake': 2006 ]

是的,你没有对答案提供反馈。 - Tarun Lalwani
请使用此链接 https://chat.stackoverflow.com/rooms/170229/discussion-between-tarun-lalwani-and-david-brierton - Tarun Lalwani

8
您可以使用以下内容:
data = require("./data.json")


flattenObject = (obj) => {
    let flattenKeys = {};
    for (let i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if ((typeof obj[i]) == 'object') {
            // flattenKeys[i] = obj[i];
            let flatObject = flattenObject(obj[i]);
            for (let j in flatObject) {
                if (!flatObject.hasOwnProperty(j)) continue;
                flattenKeys[i + '.' + j] = flatObject[j];
            }
        } else {
            flattenKeys[i] = obj[i];
        }
    }
    return flattenKeys;
}

console.log(flattenObject(data))

您的数组对象的第一个元素的输出如下:
{ countyCode: 12,
  'customer.addresses.0.addressType': 'R',
  'customer.addresses.0.city': 'BRADENTON',
  'customer.addresses.0.countryCode': 'US',
  'customer.addresses.0.countyCode': 12,
  'customer.addresses.0.state': 'FL',
  'customer.addresses.0.streetAddress': '819 15th Ave Dr E',
  'customer.addresses.0.zipCode': 34211,
  'customer.addresses.1.addressType': 'M',
  'customer.addresses.1.city': 'BRADENTON',
  'customer.addresses.1.countryCode': 'US',
  'customer.addresses.1.countyCode': 12,
  'customer.addresses.1.state': 'FL',
  'customer.addresses.1.streetAddress': 'PO BOX 124',
  'customer.addresses.1.zipCode': 34201,
  'customer.addresses.1.zipPlus': '124',
  'customer.customerNumber': 932874,
  'customer.customerStopFlag': false,
  'customer.customerType': 'I',
  'customer.dateOfBirth': '1936-08-05T00:00:00',
  'customer.dlExpirationDate': '2022-08-05T00:00:00',
  'customer.dlRenewalEligibilityFlag': true,
  'customer.driverLicenseNumber': 'B360722339284',
  'customer.firstName': 'David',
  'customer.lastName': 'Brierton',
  'customer.middleName': 'Hugh',
  'customer.sex': 'M' }

3
另一种解决方案:
function flatObjectToString(obj) {
    var path = [],
        nodes = {},
        parseObj = function (obj) {
            if (typeof obj == 'object') {
                if (obj instanceof Array) { //array
                    for (var i = 0, l = obj.length; i < l; i++) {
                        path.push(i);
                        parseObj(obj[i]);
                        path.pop();
                    }
                }
                else {  //object
                    for (var node in obj) {
                        path.push(node);
                        parseObj(obj[node]);
                        path.pop();
                    }
                }
            }
            else {  //value
                nodes[path.join('_')] = obj;
            }
        };

    parseObj(obj);
    return nodes;
}

console.log(JSON.stringify(flatObjectToString(data)));
  • 我使用了一个全局路径数组来存储递归路径并避免大量字符串操作。
  • 我不知道实际的JSON文件大小,所以我尝试找到一种不浪费任何垃圾的解决方案。
  • 在这种情况下,无需检查"hasOwnProperty"。

2
这里有一个易读且正确解析日期的解决方案。一些解决方案会递归地展开对象,但是没有考虑到日期也是对象。当在js对象上使用相同的展平函数时,这将成为问题,而在使用json文件时则不会。前缀插入一个或多个前缀(“my.prefixes.here.0.value”)。
export const flatten = (obj, prefix = [], current = {}) => {
  const isDate = obj instanceof Date;
  const isObject = typeof obj === "object";
  const notNull = obj !== null;
  const flattenRecursive = !isDate && isObject && notNull;

  if (flattenRecursive) {
    for (const key in obj) {
      flatten(obj[key], prefix.concat(key), current);
    }
  } else {
    current[prefix.join(".")] = obj;
  }
  return current;
};

编辑:使用来自op的示例数据输出函数的结果:

{ '0.countyCode': 12,
  '0.customer.addresses.0.addressType': 'R',
  '0.customer.addresses.0.city': 'BRADENTON',
  '0.customer.addresses.0.countryCode': 'US',
  '0.customer.addresses.0.countyCode': 12,
  '0.customer.addresses.0.foreignPostalCode': null,
  '0.customer.addresses.0.state': 'FL',
  '0.customer.addresses.0.streetAddress': '819 15th Ave Dr E',
  '0.customer.addresses.0.zipCode': 34211,
  '0.customer.addresses.0.zipPlus': null,
  '0.customer.addresses.1.addressType': 'M',
  '0.customer.addresses.1.city': 'BRADENTON',
  '0.customer.addresses.1.countryCode': 'US',
  '0.customer.addresses.1.countyCode': 12,
  '0.customer.addresses.1.foreignPostalCode': null,
  '0.customer.addresses.1.state': 'FL',
  '0.customer.addresses.1.streetAddress': 'PO BOX 124',
  '0.customer.addresses.1.zipCode': 34201,
  '0.customer.addresses.1.zipPlus': '0124',
  '0.customer.companyName': null,
  '0.customer.customerNumber': 932874,
  '0.customer.customerStopFlag': false,
  '0.customer.customerType': 'I',
  '0.customer.dateOfBirth': '1936-08-05T00:00:00',
  '0.customer.dlExpirationDate': '2022-08-05T00:00:00',
  '0.customer.dlRenewalEligibilityFlag': true,
  '0.customer.driverLicenseNumber': 'B360722339284',
  '0.customer.emailAddress': null,
  '0.customer.feidNumber': null,
  '0.customer.firstName': 'David',
  '0.customer.lastName': 'Brierton',
  '0.customer.middleName': 'Hugh',
  '0.customer.militaryExemptionFlag': null,
  '0.customer.nameSuffix': null,
  '0.customer.sex': 'M' }

2

我建议使用 flat 而不是重复造轮子。如果你真的不想这样做,你可以阅读 Hugh 的代码 这里。他支持 flatten 和 unflatten 两种功能。以下是参考的 flatten 函数:

var isBuffer = require('is-buffer')

module.exports = flatten
flatten.flatten = flatten
flatten.unflatten = unflatten

function flatten (target, opts) {
  opts = opts || {}

  var delimiter = opts.delimiter || '.'
  var maxDepth = opts.maxDepth
  var output = {}

  function step (object, prev, currentDepth) {
    currentDepth = currentDepth || 1
    Object.keys(object).forEach(function (key) {
      var value = object[key]
      var isarray = opts.safe && Array.isArray(value)
      var type = Object.prototype.toString.call(value)
      var isbuffer = isBuffer(value)
      var isobject = (
        type === '[object Object]' ||
        type === '[object Array]'
      )

      var newKey = prev
        ? prev + delimiter + key
        : key

      if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
        (!opts.maxDepth || currentDepth < maxDepth)) {
        return step(value, newKey, currentDepth + 1)
      }

      output[newKey] = value
    })
  }

  step(target)

  return output
}


1
function obj2csv(obj, opt) {
        if (typeof obj !== 'object') return null;
        opt = opt || {};
        var scopechar = opt.scopechar || '.';
        var delimeter = opt.delimeter || ',';
        if (Array.isArray(obj) === false) obj = [obj];
        var curs, name, rownum, key, queue, values = [], rows = [], headers = {}, headersArr = [];
        for (rownum = 0; rownum < obj.length; rownum++) {
            queue = [obj[rownum], ''];
            rows[rownum] = {};
            while (queue.length > 0) {
                name = queue.pop();
                curs = queue.pop();
                if (curs !== null && typeof curs === 'object') {
                    for (key in curs) {
                        if (curs.hasOwnProperty(key)) {
                            queue.push(curs[key]);
                            queue.push(name + (name ? scopechar : '') + key);
                        }
                    }
                } else {
                    if (headers[name] === undefined) headers[name] = true;
                    rows[rownum][name] = curs;
                }
            }
            values[rownum] = [];
        }
        // create csv text
        for (key in headers) {
            if (headers.hasOwnProperty(key)) {
                headersArr.push(key);
                for (rownum = 0; rownum < obj.length; rownum++) {
                    values[rownum].push(rows[rownum][key] === undefined
                                        ? ''
                                        : rows[rownum][key]);//JSON.stringify()
                }
            }
        }
        for (rownum = 0; rownum < obj.length; rownum++) {
            values[rownum] = values[rownum].join(delimeter);

        }

          return '"' + headersArr.join('"' + delimeter + '"') + '"\n' + values.join('\n');
    }

如果您有以下类似json的数据
{
  "_id": "template:user",
  "_rev": "11-d319c4ac632171d6f01c40fdef3164a5",
  "p": "user",
  "first_name": "first_name_000",
  "last_name": "last_name_000",
  "favorite_list": {
    "field": "value_000"
  },
  "list_kmorganisation": [
    {
      "siren": "siren_000",
      "partition": "partition_000",
      "id_role": "id_role_000",
      "is_default": "is_default_000",
      "is_managed": "is_managed_000",
      "is_banned": "is_managed_000",
      "ban_reason": "ban_reason_000",
      "ban_date": "ban_date_000",
      "last_connection": "last_connection_000"
    }
  ],
  "login": {
    "mail": "mail_000",
    "passwd": "passwd_000",
    "salt": "salt_000",
    "status": "status_000",
    "access": [
      {
        "log_date": "log_date_000",
        "os": "os_000",
        "version": "version_000",
        "ip_addr": "ip_addr_000",
        "screen": "screen_000"
      }
    ]
  }
}

最终结果将被压缩成如下格式: "login__access__0__screen", "login__access__0__ip_addr", "login__access__0__version", "login__access__0__os", "login__access__0__log_date", "login__status", "login__salt", "login__passwd", "login__mail", "last_name","first_name", "list_kmorganisation__1__last_connection", "list_kmorganisation__1__ban_date", "list_kmorganisation__1__ban_reason", "list_kmorganisation__1__is_banned", "list_kmorganisation__1__is_managed", "list_kmorganisation__1__is_default", "list_kmorganisation__1__id_role", "list_kmorganisation__1__partition", "list_kmorganisation__1__cmpny_name", "list_kmorganisation__1__siren", "list_kmorganisation__0__last_connection", "list_kmorganisation__0__ban_date", "list_kmorganisation__0__ban_reason", "list_kmorganisation__0__is_banned", "list_kmorganisation__0__is_managed", "list_kmorganisation__0__is_default", "list_kmorganisation__0__id_role", "list_kmorganisation__0__partition", "list_kmorganisation__0__cmpny_name", "list_kmorganisation__0__siren", "p", "_rev", "_id"(当然,所有值都在同一行中)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接