JSON到CSV展平嵌套JSON

时间:2018-04-24 19:06:59

标签: javascript json

我正在尝试将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

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 Utilities运行它:

// 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);

4 个答案:

答案 0 :(得分: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' }

答案 1 :(得分:6)

如果您正在寻找对实际代码的修复,那么您需要使用以下内容

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 ]

答案 2 :(得分: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文件的大小,所以我试图找到一个不浪费任何垃圾的解决方案
  • 无需检查&#34; hasOwnProperty&#34;在这个(你的)案例中

答案 3 :(得分:2)

我建议使用flat而不是重新发明轮子。如果你真的不想这样做,你可以阅读休的代码here。他支持扁平化和不扁平化。以下是展平函数供参考:

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
}