Parse a dot delimited numerical hierarchy index JSON












0















Suppose I have a data structure in the following format



https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



-----------
MovementOfGoods
---| 4.2. MovementOfGoods : N/A
------| 4.2.1. NumberOfMovementLines : Inteiro *
------| 4.2.2. TotalQuantityIssued : Decimal *
------| 4.2.3. StockMovement : N/A
---------| 4.2.3.1. DocumentNumber : Texto 60 *
---------| 4.2.3.2. ATCUD : Texto 100 *
---------| 4.2.3.3. DocumentStatus : N/A *
------------| 4.2.3.3.1. MovementStatus : Texto 1 *
------------| 4.2.3.3.2. MovementStatusDate : Data e Hora *
------------| 4.2.3.3.3. Reason : Texto 50
------------| 4.2.3.3.4. SourceID : Texto 30 *
------------| 4.2.3.3.5. SourceBilling : Texto 1 *
---------| 4.2.3.4. Hash : Texto 172 *
---------| 4.2.3.5. HashControl : Texto 70 *
---------| 4.2.3.6. Period : Inteiro
---------| 4.2.3.7. MovementDate : Data *
---------| 4.2.3.8. MovementType : Texto 2 *
---------| 4.2.3.9. SystemEntryDate : Data e Hora *
---------| 4.2.3.10. TransactionID : Texto 70 **
---------| 4.2.3.11. CustomerID : Texto 30 **
---------| 4.2.3.12. SupplierID : Texto 30 **
---------| 4.2.3.13. SourceID : Texto 30 *
---------| 4.2.3.14. EACCode : Texto 5
---------| 4.2.3.15. MovementComments : Texto 60
---------| 4.2.3.16. ShipTo : N/A
------------| 4.2.3.16.1. DeliveryID : Texto 255
------------| 4.2.3.16.2. DeliveryDate : Data
------------| 4.2.3.16.3. WarehouseID : Texto 50
------------| 4.2.3.16.4. LocationID : Texto 30
------------| 4.2.3.16.5. Address : N/A
---------------| 4.2.3.16.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.16.5.2. StreetName : Texto 200
---------------| 4.2.3.16.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.16.5.4. City : Texto 50 *
---------------| 4.2.3.16.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.16.5.6. Region : Texto 50
---------------| 4.2.3.16.5.7. Country : Texto 2 *
---------| 4.2.3.17. ShipFrom : N/A
------------| 4.2.3.17.1. DeliveryID : Texto 255
------------| 4.2.3.17.2. DeliveryDate : Data
------------| 4.2.3.17.3. WarehouseID : Texto 50
------------| 4.2.3.17.4. LocationID : Texto 30
------------| 4.2.3.17.5. Address : N/A
---------------| 4.2.3.17.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.17.5.2. StreetName : Texto 200
---------------| 4.2.3.17.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.17.5.4. City : Texto 50 *
---------------| 4.2.3.17.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.17.5.6. Region : Texto 20
---------------| 4.2.3.17.5.7. Country : Texto 2 *
---------| 4.2.3.18. MovementEndTime : Data e hora
---------| 4.2.3.19. MovementStartTime : Data e hora *
---------| 4.2.3.20. ATDocCodeID : Texto 200
---------| 4.2.3.21. Line : N/A *
------------| 4.2.3.21.1. LineNumber : Inteiro *
------------| 4.2.3.21.2. OrderReferences : N/A
---------------| 4.2.3.21.2.1. OriginatingON : Texto 60
---------------| 4.2.3.21.2.2. OrderDate : Data
------------| 4.2.3.21.3. ProductCode : Texto 60 *
------------| 4.2.3.21.4. ProductDescription : Texto 200 *
------------| 4.2.3.21.5. Quantity : Decimal *
------------| 4.2.3.21.6. UnitOfMeasure : Texto 20 *
------------| 4.2.3.21.7. UnitPrice : Monetário *
------------| 4.2.3.21.8. Description : Texto 200 *
------------| 4.2.3.21.9. ProductSerialNumber : N/A
---------------| 4.2.3.21.9.1. SerialNumber : Texto 100 *
------------| 4.2.3.21.10. DebitAmount : Monetário **
------------| 4.2.3.21.11. CreditAmount : Monetário **
------------| 4.2.3.21.12. Tax : N/A **
---------------| 4.2.3.21.12.1. TaxType : Texto 3 *
---------------| 4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
---------------| 4.2.3.21.12.3. TaxCode : Texto 10 *
---------------| 4.2.3.21.12.4. TaxPercentage : Decimal *
------------| 4.2.3.21.13. TaxExemptionReason : Texto 60 **
------------| 4.2.3.21.14. TaxExemptionCode : Texto 3 **
------------| 4.2.3.21.15. SettlementAmount : Monetário
------------| 4.2.3.21.16. CustomsInformation : N/A
---------------| 4.2.3.21.16.1. ARCNo : Texto 21
---------------| 4.2.3.21.16.2. IECAmount : Monetário
---------| 4.2.3.22. DocumentTotals : N/A *
------------| 4.2.3.22.1. TaxPayable : Monetário *
------------| 4.2.3.22.2. NetTotal : Monetário *
------------| 4.2.3.22.3. GrossTotal : Monetário *
------------| 4.2.3.22.4. Currency : N/A
---------------| 4.2.3.22.4.1. CurrencyCode : Texto 3 *
---------------| 4.2.3.22.4.2. CurrencyAmount : Monetário *
---------------| 4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency


Which I would like to parse into JSON with objects embedded so as to follow the hierarchy created by the numbering.



The above is a parsed version of a tsv file exported from Google Sheets, https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



I suppose an algorithm to do this would need to be aware of what "level" each line is, when a new indention is about to be made, into a new object, newer even embedded objects inside that, and then back down to root, as in



-----------
Customer
---| 2.2.1. CustomerID : Texto 30 *
---| 2.2.2. AccountID : Texto 30 *
---| 2.2.3. CustomerTaxID : Texto 30 *
---| 2.2.4. CompanyName : Texto 100 *
---| 2.2.5. Contact : Texto 50
---| 2.2.6. BillingAddress : N/A *
------| 2.2.6.1. BuildingNumber : Texto 10
------| 2.2.6.2. StreetName : Texto 200
------| 2.2.6.3. AddressDetail : Texto 210 *
------| 2.2.6.4. City : Texto 50 *
------| 2.2.6.5. PostalCode : Texto 20 *
------| 2.2.6.6. Region : Texto 50
------| 2.2.6.7. Country : Texto 12 *
---| 2.2.7. ShipToAddress : N/A
------| 2.2.7.1. BuildingNumber : Texto 10
------| 2.2.7.2. StreetName : Texto 200
------| 2.2.7.3. AddressDetail : Texto 210 *
------| 2.2.7.4. City : Texto 50 *
------| 2.2.7.5. PostalCode : Texto 20 *
------| 2.2.7.6. Region : Texto 50
------| 2.2.7.7. Country : Texto 12 *
---| 2.2.8. Telephone : Texto 20
---| 2.2.9. Fax : Texto 20
---| 2.2.10. Email : Texto 60
---| 2.2.11. Website : Texto 60
---| 2.2.12. SelfBillingIndicator : Inteiro *

[Embedded structures]
~> BillingAddress
~> ShipToAddress


for example.



My current code goes through the list and detects which are embedded structures, but fails to keep track of which of these structures should be embedded in, so hierarchy is lost, or actually only visual. After going through each sheet, the "[Embedded structures]" are listed, but flat, not in a tree.






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=692973693&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;

lines.forEach(function(line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)
if (index == 1) {
initialLevel = currentLevel;
}
if (columns[5].toLowerCase() == 'n/a') dataStructs.push(columns[2]);
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
} else if (currentLevel < lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
lastLevel = currentLevel
} else {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
}
});
console.log('n[Embedded structures]')
dataStructs.forEach(struct => {
console.log('~>', struct/* , '=', modelName+struct, '?' */)
})
console.log('n')
}
})





The code above will output






4.2. MovementOfGoods : N/A 
4.2.1. NumberOfMovementLines : Inteiro *
4.2.2. TotalQuantityIssued : Decimal *
4.2.3. StockMovement : N/A
4.2.3.1. DocumentNumber : Texto 60 *
4.2.3.2. ATCUD : Texto 100 *
4.2.3.3. DocumentStatus : N/A *
4.2.3.3.1. MovementStatus : Texto 1 *
4.2.3.3.2. MovementStatusDate : Data e Hora *
4.2.3.3.3. Reason : Texto 50
4.2.3.3.4. SourceID : Texto 30 *
4.2.3.3.5. SourceBilling : Texto 1 *
4.2.3.4. Hash : Texto 172 *
4.2.3.5. HashControl : Texto 70 *
4.2.3.6. Period : Inteiro
4.2.3.7. MovementDate : Data *
4.2.3.8. MovementType : Texto 2 *
4.2.3.9. SystemEntryDate : Data e Hora *
4.2.3.10. TransactionID : Texto 70 **
4.2.3.11. CustomerID : Texto 30 **
4.2.3.12. SupplierID : Texto 30 **
4.2.3.13. SourceID : Texto 30 *
4.2.3.14. EACCode : Texto 5
4.2.3.15. MovementComments : Texto 60
4.2.3.16. ShipTo : N/A
4.2.3.16.1. DeliveryID : Texto 255
4.2.3.16.2. DeliveryDate : Data
4.2.3.16.3. WarehouseID : Texto 50
4.2.3.16.4. LocationID : Texto 30
4.2.3.16.5. Address : N/A
4.2.3.16.5.1. BuildingNumber : Texto 10
4.2.3.16.5.2. StreetName : Texto 200
4.2.3.16.5.3. AddressDetail : Texto 210 *
4.2.3.16.5.4. City : Texto 50 *
4.2.3.16.5.5. PostalCode : Texto 20 *
4.2.3.16.5.6. Region : Texto 50
4.2.3.16.5.7. Country : Texto 2 *
4.2.3.17. ShipFrom : N/A
4.2.3.17.1. DeliveryID : Texto 255
4.2.3.17.2. DeliveryDate : Data
4.2.3.17.3. WarehouseID : Texto 50
4.2.3.17.4. LocationID : Texto 30
4.2.3.17.5. Address : N/A
4.2.3.17.5.1. BuildingNumber : Texto 10
4.2.3.17.5.2. StreetName : Texto 200
4.2.3.17.5.3. AddressDetail : Texto 210 *
4.2.3.17.5.4. City : Texto 50 *
4.2.3.17.5.5. PostalCode : Texto 20 *
4.2.3.17.5.6. Region : Texto 20
4.2.3.17.5.7. Country : Texto 2 *
4.2.3.18. MovementEndTime : Data e hora
4.2.3.19. MovementStartTime : Data e hora *
4.2.3.20. ATDocCodeID : Texto 200
4.2.3.21. Line : N/A *
4.2.3.21.1. LineNumber : Inteiro *
4.2.3.21.2. OrderReferences : N/A
4.2.3.21.2.1. OriginatingON : Texto 60
4.2.3.21.2.2. OrderDate : Data
4.2.3.21.3. ProductCode : Texto 60 *
4.2.3.21.4. ProductDescription : Texto 200 *
4.2.3.21.5. Quantity : Decimal *
4.2.3.21.6. UnitOfMeasure : Texto 20 *
4.2.3.21.7. UnitPrice : Monetário *
4.2.3.21.8. Description : Texto 200 *
4.2.3.21.9. ProductSerialNumber : N/A
4.2.3.21.9.1. SerialNumber : Texto 100 *
4.2.3.21.10. DebitAmount : Monetário **
4.2.3.21.11. CreditAmount : Monetário **
4.2.3.21.12. Tax : N/A **
4.2.3.21.12.1. TaxType : Texto 3 *
4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
4.2.3.21.12.3. TaxCode : Texto 10 *
4.2.3.21.12.4. TaxPercentage : Decimal *
4.2.3.21.13. TaxExemptionReason : Texto 60 **
4.2.3.21.14. TaxExemptionCode : Texto 3 **
4.2.3.21.15. SettlementAmount : Monetário
4.2.3.21.16. CustomsInformation : N/A
4.2.3.21.16.1. ARCNo : Texto 21
4.2.3.21.16.2. IECAmount : Monetário
4.2.3.22. DocumentTotals : N/A *
4.2.3.22.1. TaxPayable : Monetário *
4.2.3.22.2. NetTotal : Monetário *
4.2.3.22.3. GrossTotal : Monetário *
4.2.3.22.4. Currency : N/A
4.2.3.22.4.1. CurrencyCode : Texto 3 *
4.2.3.22.4.2. CurrencyAmount : Monetário *
4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency





How would one go down these lists and add the fields/properties to the right embedded objects or to the root of the object, in a tree-like form?



edit:



Using the above "Customer" as an example, with a tabular structure such as https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=700343422&single=true the desired output would be






{
"Customer": {
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50",
"2.2.6. BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10",
"2.2.6.2. StreetName": "Texto 200",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"2.2.7. ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10",
"2.2.7.2. StreetName": "Texto 200",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone ": "Texto 20",
"2.2.9. Fax": "Texto 20",
"2.2.10. Email": "Texto 60",
"2.2.11. Website": "Texto 60",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}
}





edit 2:
To be clear, the desired "outcome" is to be able to detect when nested objects start and end, as well as nesting them in the right place according to the source table. The particular columns from the table passed onto JSON could vary, as well as the actual syntax used changed from JSON to, say, rails generators. What I'm missing is the "level detection" part of the code.



edit 3:






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=700343422&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;
goneUpCounter = 0;
console.log("{")

lines.forEach(function (line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)

if (columns[5].toLowerCase() == 'n/a') {
dataStructs.push(columns[2]);
goneUpCounter += 1;
}
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log("{"" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
} else if (currentLevel < lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}
lastLevel = currentLevel
} else {

if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
if ((lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) || index + 1 == lines.length) {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + """)
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}

if (lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) {
goneUpCounter -= 1;
console.log('},')
}
}
}
});
for (let i = 0; i < goneUpCounter + 1; i++) {
console.log('}')
}
}
})





returns valid JSON, but the code is just awful.






{
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50 ",
"BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10 ",
"2.2.6.2. StreetName": "Texto 200 ",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10 ",
"2.2.7.2. StreetName": "Texto 200 ",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50 ",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone": "Texto 20 ",
"2.2.9. Fax": "Texto 20 ",
"2.2.10. Email": "Texto 60 ",
"2.2.11. Website": "Texto 60 ",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}












share|improve this question

























  • Could you please add a desired result example to the question?

    – Kosh Very
    Nov 25 '18 at 23:19











  • Just did, thank you.

    – bcsantos
    Nov 25 '18 at 23:40






  • 1





    You are better off going back to the original data file. It's tab delimited, where the second element on a line is empty, it has child nodes. Where it contains an asterisk (*), it's a child node with no children. Call the function recursively every time it hits an empty second element, and return when the index decreases. Post what you've tried.

    – RobG
    Nov 26 '18 at 2:10











  • @RobG updated code and output. the asterisk in the source tsv tables is to indicate if a filed is mandatory. the two main cues to look for in nesting objects are the "N/A" string in filed type and the actual number sequence separated by dots. This is making me want to go learn some proper CS and make library for this kind of thing. As it stands, the code above is naive, but sort of works. Input very welcome.

    – bcsantos
    Nov 26 '18 at 2:53











  • As RobG recommended, use a recursive function that greedily reads lines of the same indentation. And to make your code less ugly, build an actual data structure as a return value instead of outputting a JSON text manually.

    – Bergi
    Nov 26 '18 at 7:21
















0















Suppose I have a data structure in the following format



https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



-----------
MovementOfGoods
---| 4.2. MovementOfGoods : N/A
------| 4.2.1. NumberOfMovementLines : Inteiro *
------| 4.2.2. TotalQuantityIssued : Decimal *
------| 4.2.3. StockMovement : N/A
---------| 4.2.3.1. DocumentNumber : Texto 60 *
---------| 4.2.3.2. ATCUD : Texto 100 *
---------| 4.2.3.3. DocumentStatus : N/A *
------------| 4.2.3.3.1. MovementStatus : Texto 1 *
------------| 4.2.3.3.2. MovementStatusDate : Data e Hora *
------------| 4.2.3.3.3. Reason : Texto 50
------------| 4.2.3.3.4. SourceID : Texto 30 *
------------| 4.2.3.3.5. SourceBilling : Texto 1 *
---------| 4.2.3.4. Hash : Texto 172 *
---------| 4.2.3.5. HashControl : Texto 70 *
---------| 4.2.3.6. Period : Inteiro
---------| 4.2.3.7. MovementDate : Data *
---------| 4.2.3.8. MovementType : Texto 2 *
---------| 4.2.3.9. SystemEntryDate : Data e Hora *
---------| 4.2.3.10. TransactionID : Texto 70 **
---------| 4.2.3.11. CustomerID : Texto 30 **
---------| 4.2.3.12. SupplierID : Texto 30 **
---------| 4.2.3.13. SourceID : Texto 30 *
---------| 4.2.3.14. EACCode : Texto 5
---------| 4.2.3.15. MovementComments : Texto 60
---------| 4.2.3.16. ShipTo : N/A
------------| 4.2.3.16.1. DeliveryID : Texto 255
------------| 4.2.3.16.2. DeliveryDate : Data
------------| 4.2.3.16.3. WarehouseID : Texto 50
------------| 4.2.3.16.4. LocationID : Texto 30
------------| 4.2.3.16.5. Address : N/A
---------------| 4.2.3.16.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.16.5.2. StreetName : Texto 200
---------------| 4.2.3.16.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.16.5.4. City : Texto 50 *
---------------| 4.2.3.16.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.16.5.6. Region : Texto 50
---------------| 4.2.3.16.5.7. Country : Texto 2 *
---------| 4.2.3.17. ShipFrom : N/A
------------| 4.2.3.17.1. DeliveryID : Texto 255
------------| 4.2.3.17.2. DeliveryDate : Data
------------| 4.2.3.17.3. WarehouseID : Texto 50
------------| 4.2.3.17.4. LocationID : Texto 30
------------| 4.2.3.17.5. Address : N/A
---------------| 4.2.3.17.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.17.5.2. StreetName : Texto 200
---------------| 4.2.3.17.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.17.5.4. City : Texto 50 *
---------------| 4.2.3.17.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.17.5.6. Region : Texto 20
---------------| 4.2.3.17.5.7. Country : Texto 2 *
---------| 4.2.3.18. MovementEndTime : Data e hora
---------| 4.2.3.19. MovementStartTime : Data e hora *
---------| 4.2.3.20. ATDocCodeID : Texto 200
---------| 4.2.3.21. Line : N/A *
------------| 4.2.3.21.1. LineNumber : Inteiro *
------------| 4.2.3.21.2. OrderReferences : N/A
---------------| 4.2.3.21.2.1. OriginatingON : Texto 60
---------------| 4.2.3.21.2.2. OrderDate : Data
------------| 4.2.3.21.3. ProductCode : Texto 60 *
------------| 4.2.3.21.4. ProductDescription : Texto 200 *
------------| 4.2.3.21.5. Quantity : Decimal *
------------| 4.2.3.21.6. UnitOfMeasure : Texto 20 *
------------| 4.2.3.21.7. UnitPrice : Monetário *
------------| 4.2.3.21.8. Description : Texto 200 *
------------| 4.2.3.21.9. ProductSerialNumber : N/A
---------------| 4.2.3.21.9.1. SerialNumber : Texto 100 *
------------| 4.2.3.21.10. DebitAmount : Monetário **
------------| 4.2.3.21.11. CreditAmount : Monetário **
------------| 4.2.3.21.12. Tax : N/A **
---------------| 4.2.3.21.12.1. TaxType : Texto 3 *
---------------| 4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
---------------| 4.2.3.21.12.3. TaxCode : Texto 10 *
---------------| 4.2.3.21.12.4. TaxPercentage : Decimal *
------------| 4.2.3.21.13. TaxExemptionReason : Texto 60 **
------------| 4.2.3.21.14. TaxExemptionCode : Texto 3 **
------------| 4.2.3.21.15. SettlementAmount : Monetário
------------| 4.2.3.21.16. CustomsInformation : N/A
---------------| 4.2.3.21.16.1. ARCNo : Texto 21
---------------| 4.2.3.21.16.2. IECAmount : Monetário
---------| 4.2.3.22. DocumentTotals : N/A *
------------| 4.2.3.22.1. TaxPayable : Monetário *
------------| 4.2.3.22.2. NetTotal : Monetário *
------------| 4.2.3.22.3. GrossTotal : Monetário *
------------| 4.2.3.22.4. Currency : N/A
---------------| 4.2.3.22.4.1. CurrencyCode : Texto 3 *
---------------| 4.2.3.22.4.2. CurrencyAmount : Monetário *
---------------| 4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency


Which I would like to parse into JSON with objects embedded so as to follow the hierarchy created by the numbering.



The above is a parsed version of a tsv file exported from Google Sheets, https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



I suppose an algorithm to do this would need to be aware of what "level" each line is, when a new indention is about to be made, into a new object, newer even embedded objects inside that, and then back down to root, as in



-----------
Customer
---| 2.2.1. CustomerID : Texto 30 *
---| 2.2.2. AccountID : Texto 30 *
---| 2.2.3. CustomerTaxID : Texto 30 *
---| 2.2.4. CompanyName : Texto 100 *
---| 2.2.5. Contact : Texto 50
---| 2.2.6. BillingAddress : N/A *
------| 2.2.6.1. BuildingNumber : Texto 10
------| 2.2.6.2. StreetName : Texto 200
------| 2.2.6.3. AddressDetail : Texto 210 *
------| 2.2.6.4. City : Texto 50 *
------| 2.2.6.5. PostalCode : Texto 20 *
------| 2.2.6.6. Region : Texto 50
------| 2.2.6.7. Country : Texto 12 *
---| 2.2.7. ShipToAddress : N/A
------| 2.2.7.1. BuildingNumber : Texto 10
------| 2.2.7.2. StreetName : Texto 200
------| 2.2.7.3. AddressDetail : Texto 210 *
------| 2.2.7.4. City : Texto 50 *
------| 2.2.7.5. PostalCode : Texto 20 *
------| 2.2.7.6. Region : Texto 50
------| 2.2.7.7. Country : Texto 12 *
---| 2.2.8. Telephone : Texto 20
---| 2.2.9. Fax : Texto 20
---| 2.2.10. Email : Texto 60
---| 2.2.11. Website : Texto 60
---| 2.2.12. SelfBillingIndicator : Inteiro *

[Embedded structures]
~> BillingAddress
~> ShipToAddress


for example.



My current code goes through the list and detects which are embedded structures, but fails to keep track of which of these structures should be embedded in, so hierarchy is lost, or actually only visual. After going through each sheet, the "[Embedded structures]" are listed, but flat, not in a tree.






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=692973693&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;

lines.forEach(function(line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)
if (index == 1) {
initialLevel = currentLevel;
}
if (columns[5].toLowerCase() == 'n/a') dataStructs.push(columns[2]);
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
} else if (currentLevel < lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
lastLevel = currentLevel
} else {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
}
});
console.log('n[Embedded structures]')
dataStructs.forEach(struct => {
console.log('~>', struct/* , '=', modelName+struct, '?' */)
})
console.log('n')
}
})





The code above will output






4.2. MovementOfGoods : N/A 
4.2.1. NumberOfMovementLines : Inteiro *
4.2.2. TotalQuantityIssued : Decimal *
4.2.3. StockMovement : N/A
4.2.3.1. DocumentNumber : Texto 60 *
4.2.3.2. ATCUD : Texto 100 *
4.2.3.3. DocumentStatus : N/A *
4.2.3.3.1. MovementStatus : Texto 1 *
4.2.3.3.2. MovementStatusDate : Data e Hora *
4.2.3.3.3. Reason : Texto 50
4.2.3.3.4. SourceID : Texto 30 *
4.2.3.3.5. SourceBilling : Texto 1 *
4.2.3.4. Hash : Texto 172 *
4.2.3.5. HashControl : Texto 70 *
4.2.3.6. Period : Inteiro
4.2.3.7. MovementDate : Data *
4.2.3.8. MovementType : Texto 2 *
4.2.3.9. SystemEntryDate : Data e Hora *
4.2.3.10. TransactionID : Texto 70 **
4.2.3.11. CustomerID : Texto 30 **
4.2.3.12. SupplierID : Texto 30 **
4.2.3.13. SourceID : Texto 30 *
4.2.3.14. EACCode : Texto 5
4.2.3.15. MovementComments : Texto 60
4.2.3.16. ShipTo : N/A
4.2.3.16.1. DeliveryID : Texto 255
4.2.3.16.2. DeliveryDate : Data
4.2.3.16.3. WarehouseID : Texto 50
4.2.3.16.4. LocationID : Texto 30
4.2.3.16.5. Address : N/A
4.2.3.16.5.1. BuildingNumber : Texto 10
4.2.3.16.5.2. StreetName : Texto 200
4.2.3.16.5.3. AddressDetail : Texto 210 *
4.2.3.16.5.4. City : Texto 50 *
4.2.3.16.5.5. PostalCode : Texto 20 *
4.2.3.16.5.6. Region : Texto 50
4.2.3.16.5.7. Country : Texto 2 *
4.2.3.17. ShipFrom : N/A
4.2.3.17.1. DeliveryID : Texto 255
4.2.3.17.2. DeliveryDate : Data
4.2.3.17.3. WarehouseID : Texto 50
4.2.3.17.4. LocationID : Texto 30
4.2.3.17.5. Address : N/A
4.2.3.17.5.1. BuildingNumber : Texto 10
4.2.3.17.5.2. StreetName : Texto 200
4.2.3.17.5.3. AddressDetail : Texto 210 *
4.2.3.17.5.4. City : Texto 50 *
4.2.3.17.5.5. PostalCode : Texto 20 *
4.2.3.17.5.6. Region : Texto 20
4.2.3.17.5.7. Country : Texto 2 *
4.2.3.18. MovementEndTime : Data e hora
4.2.3.19. MovementStartTime : Data e hora *
4.2.3.20. ATDocCodeID : Texto 200
4.2.3.21. Line : N/A *
4.2.3.21.1. LineNumber : Inteiro *
4.2.3.21.2. OrderReferences : N/A
4.2.3.21.2.1. OriginatingON : Texto 60
4.2.3.21.2.2. OrderDate : Data
4.2.3.21.3. ProductCode : Texto 60 *
4.2.3.21.4. ProductDescription : Texto 200 *
4.2.3.21.5. Quantity : Decimal *
4.2.3.21.6. UnitOfMeasure : Texto 20 *
4.2.3.21.7. UnitPrice : Monetário *
4.2.3.21.8. Description : Texto 200 *
4.2.3.21.9. ProductSerialNumber : N/A
4.2.3.21.9.1. SerialNumber : Texto 100 *
4.2.3.21.10. DebitAmount : Monetário **
4.2.3.21.11. CreditAmount : Monetário **
4.2.3.21.12. Tax : N/A **
4.2.3.21.12.1. TaxType : Texto 3 *
4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
4.2.3.21.12.3. TaxCode : Texto 10 *
4.2.3.21.12.4. TaxPercentage : Decimal *
4.2.3.21.13. TaxExemptionReason : Texto 60 **
4.2.3.21.14. TaxExemptionCode : Texto 3 **
4.2.3.21.15. SettlementAmount : Monetário
4.2.3.21.16. CustomsInformation : N/A
4.2.3.21.16.1. ARCNo : Texto 21
4.2.3.21.16.2. IECAmount : Monetário
4.2.3.22. DocumentTotals : N/A *
4.2.3.22.1. TaxPayable : Monetário *
4.2.3.22.2. NetTotal : Monetário *
4.2.3.22.3. GrossTotal : Monetário *
4.2.3.22.4. Currency : N/A
4.2.3.22.4.1. CurrencyCode : Texto 3 *
4.2.3.22.4.2. CurrencyAmount : Monetário *
4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency





How would one go down these lists and add the fields/properties to the right embedded objects or to the root of the object, in a tree-like form?



edit:



Using the above "Customer" as an example, with a tabular structure such as https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=700343422&single=true the desired output would be






{
"Customer": {
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50",
"2.2.6. BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10",
"2.2.6.2. StreetName": "Texto 200",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"2.2.7. ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10",
"2.2.7.2. StreetName": "Texto 200",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone ": "Texto 20",
"2.2.9. Fax": "Texto 20",
"2.2.10. Email": "Texto 60",
"2.2.11. Website": "Texto 60",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}
}





edit 2:
To be clear, the desired "outcome" is to be able to detect when nested objects start and end, as well as nesting them in the right place according to the source table. The particular columns from the table passed onto JSON could vary, as well as the actual syntax used changed from JSON to, say, rails generators. What I'm missing is the "level detection" part of the code.



edit 3:






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=700343422&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;
goneUpCounter = 0;
console.log("{")

lines.forEach(function (line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)

if (columns[5].toLowerCase() == 'n/a') {
dataStructs.push(columns[2]);
goneUpCounter += 1;
}
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log("{"" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
} else if (currentLevel < lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}
lastLevel = currentLevel
} else {

if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
if ((lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) || index + 1 == lines.length) {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + """)
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}

if (lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) {
goneUpCounter -= 1;
console.log('},')
}
}
}
});
for (let i = 0; i < goneUpCounter + 1; i++) {
console.log('}')
}
}
})





returns valid JSON, but the code is just awful.






{
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50 ",
"BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10 ",
"2.2.6.2. StreetName": "Texto 200 ",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10 ",
"2.2.7.2. StreetName": "Texto 200 ",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50 ",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone": "Texto 20 ",
"2.2.9. Fax": "Texto 20 ",
"2.2.10. Email": "Texto 60 ",
"2.2.11. Website": "Texto 60 ",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}












share|improve this question

























  • Could you please add a desired result example to the question?

    – Kosh Very
    Nov 25 '18 at 23:19











  • Just did, thank you.

    – bcsantos
    Nov 25 '18 at 23:40






  • 1





    You are better off going back to the original data file. It's tab delimited, where the second element on a line is empty, it has child nodes. Where it contains an asterisk (*), it's a child node with no children. Call the function recursively every time it hits an empty second element, and return when the index decreases. Post what you've tried.

    – RobG
    Nov 26 '18 at 2:10











  • @RobG updated code and output. the asterisk in the source tsv tables is to indicate if a filed is mandatory. the two main cues to look for in nesting objects are the "N/A" string in filed type and the actual number sequence separated by dots. This is making me want to go learn some proper CS and make library for this kind of thing. As it stands, the code above is naive, but sort of works. Input very welcome.

    – bcsantos
    Nov 26 '18 at 2:53











  • As RobG recommended, use a recursive function that greedily reads lines of the same indentation. And to make your code less ugly, build an actual data structure as a return value instead of outputting a JSON text manually.

    – Bergi
    Nov 26 '18 at 7:21














0












0








0








Suppose I have a data structure in the following format



https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



-----------
MovementOfGoods
---| 4.2. MovementOfGoods : N/A
------| 4.2.1. NumberOfMovementLines : Inteiro *
------| 4.2.2. TotalQuantityIssued : Decimal *
------| 4.2.3. StockMovement : N/A
---------| 4.2.3.1. DocumentNumber : Texto 60 *
---------| 4.2.3.2. ATCUD : Texto 100 *
---------| 4.2.3.3. DocumentStatus : N/A *
------------| 4.2.3.3.1. MovementStatus : Texto 1 *
------------| 4.2.3.3.2. MovementStatusDate : Data e Hora *
------------| 4.2.3.3.3. Reason : Texto 50
------------| 4.2.3.3.4. SourceID : Texto 30 *
------------| 4.2.3.3.5. SourceBilling : Texto 1 *
---------| 4.2.3.4. Hash : Texto 172 *
---------| 4.2.3.5. HashControl : Texto 70 *
---------| 4.2.3.6. Period : Inteiro
---------| 4.2.3.7. MovementDate : Data *
---------| 4.2.3.8. MovementType : Texto 2 *
---------| 4.2.3.9. SystemEntryDate : Data e Hora *
---------| 4.2.3.10. TransactionID : Texto 70 **
---------| 4.2.3.11. CustomerID : Texto 30 **
---------| 4.2.3.12. SupplierID : Texto 30 **
---------| 4.2.3.13. SourceID : Texto 30 *
---------| 4.2.3.14. EACCode : Texto 5
---------| 4.2.3.15. MovementComments : Texto 60
---------| 4.2.3.16. ShipTo : N/A
------------| 4.2.3.16.1. DeliveryID : Texto 255
------------| 4.2.3.16.2. DeliveryDate : Data
------------| 4.2.3.16.3. WarehouseID : Texto 50
------------| 4.2.3.16.4. LocationID : Texto 30
------------| 4.2.3.16.5. Address : N/A
---------------| 4.2.3.16.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.16.5.2. StreetName : Texto 200
---------------| 4.2.3.16.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.16.5.4. City : Texto 50 *
---------------| 4.2.3.16.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.16.5.6. Region : Texto 50
---------------| 4.2.3.16.5.7. Country : Texto 2 *
---------| 4.2.3.17. ShipFrom : N/A
------------| 4.2.3.17.1. DeliveryID : Texto 255
------------| 4.2.3.17.2. DeliveryDate : Data
------------| 4.2.3.17.3. WarehouseID : Texto 50
------------| 4.2.3.17.4. LocationID : Texto 30
------------| 4.2.3.17.5. Address : N/A
---------------| 4.2.3.17.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.17.5.2. StreetName : Texto 200
---------------| 4.2.3.17.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.17.5.4. City : Texto 50 *
---------------| 4.2.3.17.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.17.5.6. Region : Texto 20
---------------| 4.2.3.17.5.7. Country : Texto 2 *
---------| 4.2.3.18. MovementEndTime : Data e hora
---------| 4.2.3.19. MovementStartTime : Data e hora *
---------| 4.2.3.20. ATDocCodeID : Texto 200
---------| 4.2.3.21. Line : N/A *
------------| 4.2.3.21.1. LineNumber : Inteiro *
------------| 4.2.3.21.2. OrderReferences : N/A
---------------| 4.2.3.21.2.1. OriginatingON : Texto 60
---------------| 4.2.3.21.2.2. OrderDate : Data
------------| 4.2.3.21.3. ProductCode : Texto 60 *
------------| 4.2.3.21.4. ProductDescription : Texto 200 *
------------| 4.2.3.21.5. Quantity : Decimal *
------------| 4.2.3.21.6. UnitOfMeasure : Texto 20 *
------------| 4.2.3.21.7. UnitPrice : Monetário *
------------| 4.2.3.21.8. Description : Texto 200 *
------------| 4.2.3.21.9. ProductSerialNumber : N/A
---------------| 4.2.3.21.9.1. SerialNumber : Texto 100 *
------------| 4.2.3.21.10. DebitAmount : Monetário **
------------| 4.2.3.21.11. CreditAmount : Monetário **
------------| 4.2.3.21.12. Tax : N/A **
---------------| 4.2.3.21.12.1. TaxType : Texto 3 *
---------------| 4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
---------------| 4.2.3.21.12.3. TaxCode : Texto 10 *
---------------| 4.2.3.21.12.4. TaxPercentage : Decimal *
------------| 4.2.3.21.13. TaxExemptionReason : Texto 60 **
------------| 4.2.3.21.14. TaxExemptionCode : Texto 3 **
------------| 4.2.3.21.15. SettlementAmount : Monetário
------------| 4.2.3.21.16. CustomsInformation : N/A
---------------| 4.2.3.21.16.1. ARCNo : Texto 21
---------------| 4.2.3.21.16.2. IECAmount : Monetário
---------| 4.2.3.22. DocumentTotals : N/A *
------------| 4.2.3.22.1. TaxPayable : Monetário *
------------| 4.2.3.22.2. NetTotal : Monetário *
------------| 4.2.3.22.3. GrossTotal : Monetário *
------------| 4.2.3.22.4. Currency : N/A
---------------| 4.2.3.22.4.1. CurrencyCode : Texto 3 *
---------------| 4.2.3.22.4.2. CurrencyAmount : Monetário *
---------------| 4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency


Which I would like to parse into JSON with objects embedded so as to follow the hierarchy created by the numbering.



The above is a parsed version of a tsv file exported from Google Sheets, https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



I suppose an algorithm to do this would need to be aware of what "level" each line is, when a new indention is about to be made, into a new object, newer even embedded objects inside that, and then back down to root, as in



-----------
Customer
---| 2.2.1. CustomerID : Texto 30 *
---| 2.2.2. AccountID : Texto 30 *
---| 2.2.3. CustomerTaxID : Texto 30 *
---| 2.2.4. CompanyName : Texto 100 *
---| 2.2.5. Contact : Texto 50
---| 2.2.6. BillingAddress : N/A *
------| 2.2.6.1. BuildingNumber : Texto 10
------| 2.2.6.2. StreetName : Texto 200
------| 2.2.6.3. AddressDetail : Texto 210 *
------| 2.2.6.4. City : Texto 50 *
------| 2.2.6.5. PostalCode : Texto 20 *
------| 2.2.6.6. Region : Texto 50
------| 2.2.6.7. Country : Texto 12 *
---| 2.2.7. ShipToAddress : N/A
------| 2.2.7.1. BuildingNumber : Texto 10
------| 2.2.7.2. StreetName : Texto 200
------| 2.2.7.3. AddressDetail : Texto 210 *
------| 2.2.7.4. City : Texto 50 *
------| 2.2.7.5. PostalCode : Texto 20 *
------| 2.2.7.6. Region : Texto 50
------| 2.2.7.7. Country : Texto 12 *
---| 2.2.8. Telephone : Texto 20
---| 2.2.9. Fax : Texto 20
---| 2.2.10. Email : Texto 60
---| 2.2.11. Website : Texto 60
---| 2.2.12. SelfBillingIndicator : Inteiro *

[Embedded structures]
~> BillingAddress
~> ShipToAddress


for example.



My current code goes through the list and detects which are embedded structures, but fails to keep track of which of these structures should be embedded in, so hierarchy is lost, or actually only visual. After going through each sheet, the "[Embedded structures]" are listed, but flat, not in a tree.






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=692973693&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;

lines.forEach(function(line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)
if (index == 1) {
initialLevel = currentLevel;
}
if (columns[5].toLowerCase() == 'n/a') dataStructs.push(columns[2]);
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
} else if (currentLevel < lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
lastLevel = currentLevel
} else {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
}
});
console.log('n[Embedded structures]')
dataStructs.forEach(struct => {
console.log('~>', struct/* , '=', modelName+struct, '?' */)
})
console.log('n')
}
})





The code above will output






4.2. MovementOfGoods : N/A 
4.2.1. NumberOfMovementLines : Inteiro *
4.2.2. TotalQuantityIssued : Decimal *
4.2.3. StockMovement : N/A
4.2.3.1. DocumentNumber : Texto 60 *
4.2.3.2. ATCUD : Texto 100 *
4.2.3.3. DocumentStatus : N/A *
4.2.3.3.1. MovementStatus : Texto 1 *
4.2.3.3.2. MovementStatusDate : Data e Hora *
4.2.3.3.3. Reason : Texto 50
4.2.3.3.4. SourceID : Texto 30 *
4.2.3.3.5. SourceBilling : Texto 1 *
4.2.3.4. Hash : Texto 172 *
4.2.3.5. HashControl : Texto 70 *
4.2.3.6. Period : Inteiro
4.2.3.7. MovementDate : Data *
4.2.3.8. MovementType : Texto 2 *
4.2.3.9. SystemEntryDate : Data e Hora *
4.2.3.10. TransactionID : Texto 70 **
4.2.3.11. CustomerID : Texto 30 **
4.2.3.12. SupplierID : Texto 30 **
4.2.3.13. SourceID : Texto 30 *
4.2.3.14. EACCode : Texto 5
4.2.3.15. MovementComments : Texto 60
4.2.3.16. ShipTo : N/A
4.2.3.16.1. DeliveryID : Texto 255
4.2.3.16.2. DeliveryDate : Data
4.2.3.16.3. WarehouseID : Texto 50
4.2.3.16.4. LocationID : Texto 30
4.2.3.16.5. Address : N/A
4.2.3.16.5.1. BuildingNumber : Texto 10
4.2.3.16.5.2. StreetName : Texto 200
4.2.3.16.5.3. AddressDetail : Texto 210 *
4.2.3.16.5.4. City : Texto 50 *
4.2.3.16.5.5. PostalCode : Texto 20 *
4.2.3.16.5.6. Region : Texto 50
4.2.3.16.5.7. Country : Texto 2 *
4.2.3.17. ShipFrom : N/A
4.2.3.17.1. DeliveryID : Texto 255
4.2.3.17.2. DeliveryDate : Data
4.2.3.17.3. WarehouseID : Texto 50
4.2.3.17.4. LocationID : Texto 30
4.2.3.17.5. Address : N/A
4.2.3.17.5.1. BuildingNumber : Texto 10
4.2.3.17.5.2. StreetName : Texto 200
4.2.3.17.5.3. AddressDetail : Texto 210 *
4.2.3.17.5.4. City : Texto 50 *
4.2.3.17.5.5. PostalCode : Texto 20 *
4.2.3.17.5.6. Region : Texto 20
4.2.3.17.5.7. Country : Texto 2 *
4.2.3.18. MovementEndTime : Data e hora
4.2.3.19. MovementStartTime : Data e hora *
4.2.3.20. ATDocCodeID : Texto 200
4.2.3.21. Line : N/A *
4.2.3.21.1. LineNumber : Inteiro *
4.2.3.21.2. OrderReferences : N/A
4.2.3.21.2.1. OriginatingON : Texto 60
4.2.3.21.2.2. OrderDate : Data
4.2.3.21.3. ProductCode : Texto 60 *
4.2.3.21.4. ProductDescription : Texto 200 *
4.2.3.21.5. Quantity : Decimal *
4.2.3.21.6. UnitOfMeasure : Texto 20 *
4.2.3.21.7. UnitPrice : Monetário *
4.2.3.21.8. Description : Texto 200 *
4.2.3.21.9. ProductSerialNumber : N/A
4.2.3.21.9.1. SerialNumber : Texto 100 *
4.2.3.21.10. DebitAmount : Monetário **
4.2.3.21.11. CreditAmount : Monetário **
4.2.3.21.12. Tax : N/A **
4.2.3.21.12.1. TaxType : Texto 3 *
4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
4.2.3.21.12.3. TaxCode : Texto 10 *
4.2.3.21.12.4. TaxPercentage : Decimal *
4.2.3.21.13. TaxExemptionReason : Texto 60 **
4.2.3.21.14. TaxExemptionCode : Texto 3 **
4.2.3.21.15. SettlementAmount : Monetário
4.2.3.21.16. CustomsInformation : N/A
4.2.3.21.16.1. ARCNo : Texto 21
4.2.3.21.16.2. IECAmount : Monetário
4.2.3.22. DocumentTotals : N/A *
4.2.3.22.1. TaxPayable : Monetário *
4.2.3.22.2. NetTotal : Monetário *
4.2.3.22.3. GrossTotal : Monetário *
4.2.3.22.4. Currency : N/A
4.2.3.22.4.1. CurrencyCode : Texto 3 *
4.2.3.22.4.2. CurrencyAmount : Monetário *
4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency





How would one go down these lists and add the fields/properties to the right embedded objects or to the root of the object, in a tree-like form?



edit:



Using the above "Customer" as an example, with a tabular structure such as https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=700343422&single=true the desired output would be






{
"Customer": {
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50",
"2.2.6. BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10",
"2.2.6.2. StreetName": "Texto 200",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"2.2.7. ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10",
"2.2.7.2. StreetName": "Texto 200",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone ": "Texto 20",
"2.2.9. Fax": "Texto 20",
"2.2.10. Email": "Texto 60",
"2.2.11. Website": "Texto 60",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}
}





edit 2:
To be clear, the desired "outcome" is to be able to detect when nested objects start and end, as well as nesting them in the right place according to the source table. The particular columns from the table passed onto JSON could vary, as well as the actual syntax used changed from JSON to, say, rails generators. What I'm missing is the "level detection" part of the code.



edit 3:






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=700343422&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;
goneUpCounter = 0;
console.log("{")

lines.forEach(function (line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)

if (columns[5].toLowerCase() == 'n/a') {
dataStructs.push(columns[2]);
goneUpCounter += 1;
}
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log("{"" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
} else if (currentLevel < lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}
lastLevel = currentLevel
} else {

if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
if ((lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) || index + 1 == lines.length) {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + """)
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}

if (lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) {
goneUpCounter -= 1;
console.log('},')
}
}
}
});
for (let i = 0; i < goneUpCounter + 1; i++) {
console.log('}')
}
}
})





returns valid JSON, but the code is just awful.






{
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50 ",
"BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10 ",
"2.2.6.2. StreetName": "Texto 200 ",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10 ",
"2.2.7.2. StreetName": "Texto 200 ",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50 ",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone": "Texto 20 ",
"2.2.9. Fax": "Texto 20 ",
"2.2.10. Email": "Texto 60 ",
"2.2.11. Website": "Texto 60 ",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}












share|improve this question
















Suppose I have a data structure in the following format



https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



-----------
MovementOfGoods
---| 4.2. MovementOfGoods : N/A
------| 4.2.1. NumberOfMovementLines : Inteiro *
------| 4.2.2. TotalQuantityIssued : Decimal *
------| 4.2.3. StockMovement : N/A
---------| 4.2.3.1. DocumentNumber : Texto 60 *
---------| 4.2.3.2. ATCUD : Texto 100 *
---------| 4.2.3.3. DocumentStatus : N/A *
------------| 4.2.3.3.1. MovementStatus : Texto 1 *
------------| 4.2.3.3.2. MovementStatusDate : Data e Hora *
------------| 4.2.3.3.3. Reason : Texto 50
------------| 4.2.3.3.4. SourceID : Texto 30 *
------------| 4.2.3.3.5. SourceBilling : Texto 1 *
---------| 4.2.3.4. Hash : Texto 172 *
---------| 4.2.3.5. HashControl : Texto 70 *
---------| 4.2.3.6. Period : Inteiro
---------| 4.2.3.7. MovementDate : Data *
---------| 4.2.3.8. MovementType : Texto 2 *
---------| 4.2.3.9. SystemEntryDate : Data e Hora *
---------| 4.2.3.10. TransactionID : Texto 70 **
---------| 4.2.3.11. CustomerID : Texto 30 **
---------| 4.2.3.12. SupplierID : Texto 30 **
---------| 4.2.3.13. SourceID : Texto 30 *
---------| 4.2.3.14. EACCode : Texto 5
---------| 4.2.3.15. MovementComments : Texto 60
---------| 4.2.3.16. ShipTo : N/A
------------| 4.2.3.16.1. DeliveryID : Texto 255
------------| 4.2.3.16.2. DeliveryDate : Data
------------| 4.2.3.16.3. WarehouseID : Texto 50
------------| 4.2.3.16.4. LocationID : Texto 30
------------| 4.2.3.16.5. Address : N/A
---------------| 4.2.3.16.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.16.5.2. StreetName : Texto 200
---------------| 4.2.3.16.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.16.5.4. City : Texto 50 *
---------------| 4.2.3.16.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.16.5.6. Region : Texto 50
---------------| 4.2.3.16.5.7. Country : Texto 2 *
---------| 4.2.3.17. ShipFrom : N/A
------------| 4.2.3.17.1. DeliveryID : Texto 255
------------| 4.2.3.17.2. DeliveryDate : Data
------------| 4.2.3.17.3. WarehouseID : Texto 50
------------| 4.2.3.17.4. LocationID : Texto 30
------------| 4.2.3.17.5. Address : N/A
---------------| 4.2.3.17.5.1. BuildingNumber : Texto 10
---------------| 4.2.3.17.5.2. StreetName : Texto 200
---------------| 4.2.3.17.5.3. AddressDetail : Texto 210 *
---------------| 4.2.3.17.5.4. City : Texto 50 *
---------------| 4.2.3.17.5.5. PostalCode : Texto 20 *
---------------| 4.2.3.17.5.6. Region : Texto 20
---------------| 4.2.3.17.5.7. Country : Texto 2 *
---------| 4.2.3.18. MovementEndTime : Data e hora
---------| 4.2.3.19. MovementStartTime : Data e hora *
---------| 4.2.3.20. ATDocCodeID : Texto 200
---------| 4.2.3.21. Line : N/A *
------------| 4.2.3.21.1. LineNumber : Inteiro *
------------| 4.2.3.21.2. OrderReferences : N/A
---------------| 4.2.3.21.2.1. OriginatingON : Texto 60
---------------| 4.2.3.21.2.2. OrderDate : Data
------------| 4.2.3.21.3. ProductCode : Texto 60 *
------------| 4.2.3.21.4. ProductDescription : Texto 200 *
------------| 4.2.3.21.5. Quantity : Decimal *
------------| 4.2.3.21.6. UnitOfMeasure : Texto 20 *
------------| 4.2.3.21.7. UnitPrice : Monetário *
------------| 4.2.3.21.8. Description : Texto 200 *
------------| 4.2.3.21.9. ProductSerialNumber : N/A
---------------| 4.2.3.21.9.1. SerialNumber : Texto 100 *
------------| 4.2.3.21.10. DebitAmount : Monetário **
------------| 4.2.3.21.11. CreditAmount : Monetário **
------------| 4.2.3.21.12. Tax : N/A **
---------------| 4.2.3.21.12.1. TaxType : Texto 3 *
---------------| 4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
---------------| 4.2.3.21.12.3. TaxCode : Texto 10 *
---------------| 4.2.3.21.12.4. TaxPercentage : Decimal *
------------| 4.2.3.21.13. TaxExemptionReason : Texto 60 **
------------| 4.2.3.21.14. TaxExemptionCode : Texto 3 **
------------| 4.2.3.21.15. SettlementAmount : Monetário
------------| 4.2.3.21.16. CustomsInformation : N/A
---------------| 4.2.3.21.16.1. ARCNo : Texto 21
---------------| 4.2.3.21.16.2. IECAmount : Monetário
---------| 4.2.3.22. DocumentTotals : N/A *
------------| 4.2.3.22.1. TaxPayable : Monetário *
------------| 4.2.3.22.2. NetTotal : Monetário *
------------| 4.2.3.22.3. GrossTotal : Monetário *
------------| 4.2.3.22.4. Currency : N/A
---------------| 4.2.3.22.4.1. CurrencyCode : Texto 3 *
---------------| 4.2.3.22.4.2. CurrencyAmount : Monetário *
---------------| 4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency


Which I would like to parse into JSON with objects embedded so as to follow the hierarchy created by the numbering.



The above is a parsed version of a tsv file exported from Google Sheets, https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=692973693&single=true



I suppose an algorithm to do this would need to be aware of what "level" each line is, when a new indention is about to be made, into a new object, newer even embedded objects inside that, and then back down to root, as in



-----------
Customer
---| 2.2.1. CustomerID : Texto 30 *
---| 2.2.2. AccountID : Texto 30 *
---| 2.2.3. CustomerTaxID : Texto 30 *
---| 2.2.4. CompanyName : Texto 100 *
---| 2.2.5. Contact : Texto 50
---| 2.2.6. BillingAddress : N/A *
------| 2.2.6.1. BuildingNumber : Texto 10
------| 2.2.6.2. StreetName : Texto 200
------| 2.2.6.3. AddressDetail : Texto 210 *
------| 2.2.6.4. City : Texto 50 *
------| 2.2.6.5. PostalCode : Texto 20 *
------| 2.2.6.6. Region : Texto 50
------| 2.2.6.7. Country : Texto 12 *
---| 2.2.7. ShipToAddress : N/A
------| 2.2.7.1. BuildingNumber : Texto 10
------| 2.2.7.2. StreetName : Texto 200
------| 2.2.7.3. AddressDetail : Texto 210 *
------| 2.2.7.4. City : Texto 50 *
------| 2.2.7.5. PostalCode : Texto 20 *
------| 2.2.7.6. Region : Texto 50
------| 2.2.7.7. Country : Texto 12 *
---| 2.2.8. Telephone : Texto 20
---| 2.2.9. Fax : Texto 20
---| 2.2.10. Email : Texto 60
---| 2.2.11. Website : Texto 60
---| 2.2.12. SelfBillingIndicator : Inteiro *

[Embedded structures]
~> BillingAddress
~> ShipToAddress


for example.



My current code goes through the list and detects which are embedded structures, but fails to keep track of which of these structures should be embedded in, so hierarchy is lost, or actually only visual. After going through each sheet, the "[Embedded structures]" are listed, but flat, not in a tree.






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=692973693&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;

lines.forEach(function(line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)
if (index == 1) {
initialLevel = currentLevel;
}
if (columns[5].toLowerCase() == 'n/a') dataStructs.push(columns[2]);
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
} else if (currentLevel < lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
lastLevel = currentLevel
} else {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
}
});
console.log('n[Embedded structures]')
dataStructs.forEach(struct => {
console.log('~>', struct/* , '=', modelName+struct, '?' */)
})
console.log('n')
}
})





The code above will output






4.2. MovementOfGoods : N/A 
4.2.1. NumberOfMovementLines : Inteiro *
4.2.2. TotalQuantityIssued : Decimal *
4.2.3. StockMovement : N/A
4.2.3.1. DocumentNumber : Texto 60 *
4.2.3.2. ATCUD : Texto 100 *
4.2.3.3. DocumentStatus : N/A *
4.2.3.3.1. MovementStatus : Texto 1 *
4.2.3.3.2. MovementStatusDate : Data e Hora *
4.2.3.3.3. Reason : Texto 50
4.2.3.3.4. SourceID : Texto 30 *
4.2.3.3.5. SourceBilling : Texto 1 *
4.2.3.4. Hash : Texto 172 *
4.2.3.5. HashControl : Texto 70 *
4.2.3.6. Period : Inteiro
4.2.3.7. MovementDate : Data *
4.2.3.8. MovementType : Texto 2 *
4.2.3.9. SystemEntryDate : Data e Hora *
4.2.3.10. TransactionID : Texto 70 **
4.2.3.11. CustomerID : Texto 30 **
4.2.3.12. SupplierID : Texto 30 **
4.2.3.13. SourceID : Texto 30 *
4.2.3.14. EACCode : Texto 5
4.2.3.15. MovementComments : Texto 60
4.2.3.16. ShipTo : N/A
4.2.3.16.1. DeliveryID : Texto 255
4.2.3.16.2. DeliveryDate : Data
4.2.3.16.3. WarehouseID : Texto 50
4.2.3.16.4. LocationID : Texto 30
4.2.3.16.5. Address : N/A
4.2.3.16.5.1. BuildingNumber : Texto 10
4.2.3.16.5.2. StreetName : Texto 200
4.2.3.16.5.3. AddressDetail : Texto 210 *
4.2.3.16.5.4. City : Texto 50 *
4.2.3.16.5.5. PostalCode : Texto 20 *
4.2.3.16.5.6. Region : Texto 50
4.2.3.16.5.7. Country : Texto 2 *
4.2.3.17. ShipFrom : N/A
4.2.3.17.1. DeliveryID : Texto 255
4.2.3.17.2. DeliveryDate : Data
4.2.3.17.3. WarehouseID : Texto 50
4.2.3.17.4. LocationID : Texto 30
4.2.3.17.5. Address : N/A
4.2.3.17.5.1. BuildingNumber : Texto 10
4.2.3.17.5.2. StreetName : Texto 200
4.2.3.17.5.3. AddressDetail : Texto 210 *
4.2.3.17.5.4. City : Texto 50 *
4.2.3.17.5.5. PostalCode : Texto 20 *
4.2.3.17.5.6. Region : Texto 20
4.2.3.17.5.7. Country : Texto 2 *
4.2.3.18. MovementEndTime : Data e hora
4.2.3.19. MovementStartTime : Data e hora *
4.2.3.20. ATDocCodeID : Texto 200
4.2.3.21. Line : N/A *
4.2.3.21.1. LineNumber : Inteiro *
4.2.3.21.2. OrderReferences : N/A
4.2.3.21.2.1. OriginatingON : Texto 60
4.2.3.21.2.2. OrderDate : Data
4.2.3.21.3. ProductCode : Texto 60 *
4.2.3.21.4. ProductDescription : Texto 200 *
4.2.3.21.5. Quantity : Decimal *
4.2.3.21.6. UnitOfMeasure : Texto 20 *
4.2.3.21.7. UnitPrice : Monetário *
4.2.3.21.8. Description : Texto 200 *
4.2.3.21.9. ProductSerialNumber : N/A
4.2.3.21.9.1. SerialNumber : Texto 100 *
4.2.3.21.10. DebitAmount : Monetário **
4.2.3.21.11. CreditAmount : Monetário **
4.2.3.21.12. Tax : N/A **
4.2.3.21.12.1. TaxType : Texto 3 *
4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
4.2.3.21.12.3. TaxCode : Texto 10 *
4.2.3.21.12.4. TaxPercentage : Decimal *
4.2.3.21.13. TaxExemptionReason : Texto 60 **
4.2.3.21.14. TaxExemptionCode : Texto 3 **
4.2.3.21.15. SettlementAmount : Monetário
4.2.3.21.16. CustomsInformation : N/A
4.2.3.21.16.1. ARCNo : Texto 21
4.2.3.21.16.2. IECAmount : Monetário
4.2.3.22. DocumentTotals : N/A *
4.2.3.22.1. TaxPayable : Monetário *
4.2.3.22.2. NetTotal : Monetário *
4.2.3.22.3. GrossTotal : Monetário *
4.2.3.22.4. Currency : N/A
4.2.3.22.4.1. CurrencyCode : Texto 3 *
4.2.3.22.4.2. CurrencyAmount : Monetário *
4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency





How would one go down these lists and add the fields/properties to the right embedded objects or to the root of the object, in a tree-like form?



edit:



Using the above "Customer" as an example, with a tabular structure such as https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pubhtml?gid=700343422&single=true the desired output would be






{
"Customer": {
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50",
"2.2.6. BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10",
"2.2.6.2. StreetName": "Texto 200",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"2.2.7. ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10",
"2.2.7.2. StreetName": "Texto 200",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone ": "Texto 20",
"2.2.9. Fax": "Texto 20",
"2.2.10. Email": "Texto 60",
"2.2.11. Website": "Texto 60",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}
}





edit 2:
To be clear, the desired "outcome" is to be able to detect when nested objects start and end, as well as nesting them in the right place according to the source table. The particular columns from the table passed onto JSON could vary, as well as the actual syntax used changed from JSON to, say, rails generators. What I'm missing is the "level detection" part of the code.



edit 3:






var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=700343422&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;
goneUpCounter = 0;
console.log("{")

lines.forEach(function (line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)

if (columns[5].toLowerCase() == 'n/a') {
dataStructs.push(columns[2]);
goneUpCounter += 1;
}
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log("{"" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
} else if (currentLevel < lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}
lastLevel = currentLevel
} else {

if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
if ((lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) || index + 1 == lines.length) {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + """)
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}

if (lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) {
goneUpCounter -= 1;
console.log('},')
}
}
}
});
for (let i = 0; i < goneUpCounter + 1; i++) {
console.log('}')
}
}
})





returns valid JSON, but the code is just awful.






{
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50 ",
"BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10 ",
"2.2.6.2. StreetName": "Texto 200 ",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10 ",
"2.2.7.2. StreetName": "Texto 200 ",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50 ",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone": "Texto 20 ",
"2.2.9. Fax": "Texto 20 ",
"2.2.10. Email": "Texto 60 ",
"2.2.11. Website": "Texto 60 ",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}








var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=692973693&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;

lines.forEach(function(line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)
if (index == 1) {
initialLevel = currentLevel;
}
if (columns[5].toLowerCase() == 'n/a') dataStructs.push(columns[2]);
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
} else if (currentLevel < lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
lastLevel = currentLevel
} else {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
}
});
console.log('n[Embedded structures]')
dataStructs.forEach(struct => {
console.log('~>', struct/* , '=', modelName+struct, '?' */)
})
console.log('n')
}
})





var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=692973693&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;

lines.forEach(function(line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)
if (index == 1) {
initialLevel = currentLevel;
}
if (columns[5].toLowerCase() == 'n/a') dataStructs.push(columns[2]);
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
} else if (currentLevel < lastLevel) {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
lastLevel = currentLevel
} else {
console.log(columns[0].trim(), columns[2].trim(), ':', columns[5].trim(), columns[1].trim())
}
}
});
console.log('n[Embedded structures]')
dataStructs.forEach(struct => {
console.log('~>', struct/* , '=', modelName+struct, '?' */)
})
console.log('n')
}
})





4.2. MovementOfGoods : N/A 
4.2.1. NumberOfMovementLines : Inteiro *
4.2.2. TotalQuantityIssued : Decimal *
4.2.3. StockMovement : N/A
4.2.3.1. DocumentNumber : Texto 60 *
4.2.3.2. ATCUD : Texto 100 *
4.2.3.3. DocumentStatus : N/A *
4.2.3.3.1. MovementStatus : Texto 1 *
4.2.3.3.2. MovementStatusDate : Data e Hora *
4.2.3.3.3. Reason : Texto 50
4.2.3.3.4. SourceID : Texto 30 *
4.2.3.3.5. SourceBilling : Texto 1 *
4.2.3.4. Hash : Texto 172 *
4.2.3.5. HashControl : Texto 70 *
4.2.3.6. Period : Inteiro
4.2.3.7. MovementDate : Data *
4.2.3.8. MovementType : Texto 2 *
4.2.3.9. SystemEntryDate : Data e Hora *
4.2.3.10. TransactionID : Texto 70 **
4.2.3.11. CustomerID : Texto 30 **
4.2.3.12. SupplierID : Texto 30 **
4.2.3.13. SourceID : Texto 30 *
4.2.3.14. EACCode : Texto 5
4.2.3.15. MovementComments : Texto 60
4.2.3.16. ShipTo : N/A
4.2.3.16.1. DeliveryID : Texto 255
4.2.3.16.2. DeliveryDate : Data
4.2.3.16.3. WarehouseID : Texto 50
4.2.3.16.4. LocationID : Texto 30
4.2.3.16.5. Address : N/A
4.2.3.16.5.1. BuildingNumber : Texto 10
4.2.3.16.5.2. StreetName : Texto 200
4.2.3.16.5.3. AddressDetail : Texto 210 *
4.2.3.16.5.4. City : Texto 50 *
4.2.3.16.5.5. PostalCode : Texto 20 *
4.2.3.16.5.6. Region : Texto 50
4.2.3.16.5.7. Country : Texto 2 *
4.2.3.17. ShipFrom : N/A
4.2.3.17.1. DeliveryID : Texto 255
4.2.3.17.2. DeliveryDate : Data
4.2.3.17.3. WarehouseID : Texto 50
4.2.3.17.4. LocationID : Texto 30
4.2.3.17.5. Address : N/A
4.2.3.17.5.1. BuildingNumber : Texto 10
4.2.3.17.5.2. StreetName : Texto 200
4.2.3.17.5.3. AddressDetail : Texto 210 *
4.2.3.17.5.4. City : Texto 50 *
4.2.3.17.5.5. PostalCode : Texto 20 *
4.2.3.17.5.6. Region : Texto 20
4.2.3.17.5.7. Country : Texto 2 *
4.2.3.18. MovementEndTime : Data e hora
4.2.3.19. MovementStartTime : Data e hora *
4.2.3.20. ATDocCodeID : Texto 200
4.2.3.21. Line : N/A *
4.2.3.21.1. LineNumber : Inteiro *
4.2.3.21.2. OrderReferences : N/A
4.2.3.21.2.1. OriginatingON : Texto 60
4.2.3.21.2.2. OrderDate : Data
4.2.3.21.3. ProductCode : Texto 60 *
4.2.3.21.4. ProductDescription : Texto 200 *
4.2.3.21.5. Quantity : Decimal *
4.2.3.21.6. UnitOfMeasure : Texto 20 *
4.2.3.21.7. UnitPrice : Monetário *
4.2.3.21.8. Description : Texto 200 *
4.2.3.21.9. ProductSerialNumber : N/A
4.2.3.21.9.1. SerialNumber : Texto 100 *
4.2.3.21.10. DebitAmount : Monetário **
4.2.3.21.11. CreditAmount : Monetário **
4.2.3.21.12. Tax : N/A **
4.2.3.21.12.1. TaxType : Texto 3 *
4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
4.2.3.21.12.3. TaxCode : Texto 10 *
4.2.3.21.12.4. TaxPercentage : Decimal *
4.2.3.21.13. TaxExemptionReason : Texto 60 **
4.2.3.21.14. TaxExemptionCode : Texto 3 **
4.2.3.21.15. SettlementAmount : Monetário
4.2.3.21.16. CustomsInformation : N/A
4.2.3.21.16.1. ARCNo : Texto 21
4.2.3.21.16.2. IECAmount : Monetário
4.2.3.22. DocumentTotals : N/A *
4.2.3.22.1. TaxPayable : Monetário *
4.2.3.22.2. NetTotal : Monetário *
4.2.3.22.3. GrossTotal : Monetário *
4.2.3.22.4. Currency : N/A
4.2.3.22.4.1. CurrencyCode : Texto 3 *
4.2.3.22.4.2. CurrencyAmount : Monetário *
4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency





4.2. MovementOfGoods : N/A 
4.2.1. NumberOfMovementLines : Inteiro *
4.2.2. TotalQuantityIssued : Decimal *
4.2.3. StockMovement : N/A
4.2.3.1. DocumentNumber : Texto 60 *
4.2.3.2. ATCUD : Texto 100 *
4.2.3.3. DocumentStatus : N/A *
4.2.3.3.1. MovementStatus : Texto 1 *
4.2.3.3.2. MovementStatusDate : Data e Hora *
4.2.3.3.3. Reason : Texto 50
4.2.3.3.4. SourceID : Texto 30 *
4.2.3.3.5. SourceBilling : Texto 1 *
4.2.3.4. Hash : Texto 172 *
4.2.3.5. HashControl : Texto 70 *
4.2.3.6. Period : Inteiro
4.2.3.7. MovementDate : Data *
4.2.3.8. MovementType : Texto 2 *
4.2.3.9. SystemEntryDate : Data e Hora *
4.2.3.10. TransactionID : Texto 70 **
4.2.3.11. CustomerID : Texto 30 **
4.2.3.12. SupplierID : Texto 30 **
4.2.3.13. SourceID : Texto 30 *
4.2.3.14. EACCode : Texto 5
4.2.3.15. MovementComments : Texto 60
4.2.3.16. ShipTo : N/A
4.2.3.16.1. DeliveryID : Texto 255
4.2.3.16.2. DeliveryDate : Data
4.2.3.16.3. WarehouseID : Texto 50
4.2.3.16.4. LocationID : Texto 30
4.2.3.16.5. Address : N/A
4.2.3.16.5.1. BuildingNumber : Texto 10
4.2.3.16.5.2. StreetName : Texto 200
4.2.3.16.5.3. AddressDetail : Texto 210 *
4.2.3.16.5.4. City : Texto 50 *
4.2.3.16.5.5. PostalCode : Texto 20 *
4.2.3.16.5.6. Region : Texto 50
4.2.3.16.5.7. Country : Texto 2 *
4.2.3.17. ShipFrom : N/A
4.2.3.17.1. DeliveryID : Texto 255
4.2.3.17.2. DeliveryDate : Data
4.2.3.17.3. WarehouseID : Texto 50
4.2.3.17.4. LocationID : Texto 30
4.2.3.17.5. Address : N/A
4.2.3.17.5.1. BuildingNumber : Texto 10
4.2.3.17.5.2. StreetName : Texto 200
4.2.3.17.5.3. AddressDetail : Texto 210 *
4.2.3.17.5.4. City : Texto 50 *
4.2.3.17.5.5. PostalCode : Texto 20 *
4.2.3.17.5.6. Region : Texto 20
4.2.3.17.5.7. Country : Texto 2 *
4.2.3.18. MovementEndTime : Data e hora
4.2.3.19. MovementStartTime : Data e hora *
4.2.3.20. ATDocCodeID : Texto 200
4.2.3.21. Line : N/A *
4.2.3.21.1. LineNumber : Inteiro *
4.2.3.21.2. OrderReferences : N/A
4.2.3.21.2.1. OriginatingON : Texto 60
4.2.3.21.2.2. OrderDate : Data
4.2.3.21.3. ProductCode : Texto 60 *
4.2.3.21.4. ProductDescription : Texto 200 *
4.2.3.21.5. Quantity : Decimal *
4.2.3.21.6. UnitOfMeasure : Texto 20 *
4.2.3.21.7. UnitPrice : Monetário *
4.2.3.21.8. Description : Texto 200 *
4.2.3.21.9. ProductSerialNumber : N/A
4.2.3.21.9.1. SerialNumber : Texto 100 *
4.2.3.21.10. DebitAmount : Monetário **
4.2.3.21.11. CreditAmount : Monetário **
4.2.3.21.12. Tax : N/A **
4.2.3.21.12.1. TaxType : Texto 3 *
4.2.3.21.12.2. TaxCountryRegion : Texto 5 *
4.2.3.21.12.3. TaxCode : Texto 10 *
4.2.3.21.12.4. TaxPercentage : Decimal *
4.2.3.21.13. TaxExemptionReason : Texto 60 **
4.2.3.21.14. TaxExemptionCode : Texto 3 **
4.2.3.21.15. SettlementAmount : Monetário
4.2.3.21.16. CustomsInformation : N/A
4.2.3.21.16.1. ARCNo : Texto 21
4.2.3.21.16.2. IECAmount : Monetário
4.2.3.22. DocumentTotals : N/A *
4.2.3.22.1. TaxPayable : Monetário *
4.2.3.22.2. NetTotal : Monetário *
4.2.3.22.3. GrossTotal : Monetário *
4.2.3.22.4. Currency : N/A
4.2.3.22.4.1. CurrencyCode : Texto 3 *
4.2.3.22.4.2. CurrencyAmount : Monetário *
4.2.3.22.4.3. ExchangeRate : Decimal *

[Embedded structures]
~> MovementOfGoods
~> StockMovement
~> DocumentStatus
~> ShipTo
~> Address
~> ShipFrom
~> Address
~> Line
~> OrderReferences
~> ProductSerialNumber
~> Tax
~> CustomsInformation
~> DocumentTotals
~> Currency





{
"Customer": {
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50",
"2.2.6. BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10",
"2.2.6.2. StreetName": "Texto 200",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"2.2.7. ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10",
"2.2.7.2. StreetName": "Texto 200",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone ": "Texto 20",
"2.2.9. Fax": "Texto 20",
"2.2.10. Email": "Texto 60",
"2.2.11. Website": "Texto 60",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}
}





{
"Customer": {
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50",
"2.2.6. BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10",
"2.2.6.2. StreetName": "Texto 200",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"2.2.7. ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10",
"2.2.7.2. StreetName": "Texto 200",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone ": "Texto 20",
"2.2.9. Fax": "Texto 20",
"2.2.10. Email": "Texto 60",
"2.2.11. Website": "Texto 60",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}
}





var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=700343422&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;
goneUpCounter = 0;
console.log("{")

lines.forEach(function (line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)

if (columns[5].toLowerCase() == 'n/a') {
dataStructs.push(columns[2]);
goneUpCounter += 1;
}
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log("{"" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
} else if (currentLevel < lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}
lastLevel = currentLevel
} else {

if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
if ((lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) || index + 1 == lines.length) {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + """)
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}

if (lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) {
goneUpCounter -= 1;
console.log('},')
}
}
}
});
for (let i = 0; i < goneUpCounter + 1; i++) {
console.log('}')
}
}
})





var request = require('request')

url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTFO_MoPwZmvKZOWT2J7kjGr9OC4uUb06zGxOdmXJ8h3FZ2Q0tpsoYH653Pm5mdNcM4Fs7KdlGWSkEy/pub?gid=700343422&single=true&output=tsv"

request(url, function (error, response, body) {
if (body != undefined) {

let lines = body.split('r')
let lastLevel = 0
let dataStructs = ;
goneUpCounter = 0;
console.log("{")

lines.forEach(function (line, index) {
if (index != 0) {
columns = line.split('t')
let currentLevel = (columns[0].trim().split(".").length - 1)

if (columns[5].toLowerCase() == 'n/a') {
dataStructs.push(columns[2]);
goneUpCounter += 1;
}
if (currentLevel !== lastLevel) {
if (currentLevel > lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log("{"" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
} else if (currentLevel < lastLevel) {
if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}
lastLevel = currentLevel
} else {

if (columns[5].toLowerCase() == 'n/a') {
console.log(""" + columns[2].trim() + "" : {")
} else {
if ((lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) || index + 1 == lines.length) {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + """)
} else {
console.log(""" + columns[0].trim(), columns[2].trim() + """, ':', """ + columns[5].trim(), columns[1].trim() + "",")
}
}

if (lines[index + 1] != undefined && lines[index + 1].split('t')[0].trim().split('.').length - 1 < lastLevel) {
goneUpCounter -= 1;
console.log('},')
}
}
}
});
for (let i = 0; i < goneUpCounter + 1; i++) {
console.log('}')
}
}
})





{
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50 ",
"BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10 ",
"2.2.6.2. StreetName": "Texto 200 ",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10 ",
"2.2.7.2. StreetName": "Texto 200 ",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50 ",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone": "Texto 20 ",
"2.2.9. Fax": "Texto 20 ",
"2.2.10. Email": "Texto 60 ",
"2.2.11. Website": "Texto 60 ",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}





{
"2.2.1. CustomerID": "Texto 30 *",
"2.2.2. AccountID": "Texto 30 *",
"2.2.3. CustomerTaxID": "Texto 30 *",
"2.2.4. CompanyName": "Texto 100 *",
"2.2.5. Contact": "Texto 50 ",
"BillingAddress": {
"2.2.6.1. BuildingNumber": "Texto 10 ",
"2.2.6.2. StreetName": "Texto 200 ",
"2.2.6.3. AddressDetail": "Texto 210 *",
"2.2.6.4. City": "Texto 50 *",
"2.2.6.5. PostalCode": "Texto 20 *",
"2.2.6.6. Region": "Texto 50 ",
"2.2.6.7. Country": "Texto 12 *"
},
"ShipToAddress": {
"2.2.7.1. BuildingNumber": "Texto 10 ",
"2.2.7.2. StreetName": "Texto 200 ",
"2.2.7.3. AddressDetail": "Texto 210 *",
"2.2.7.4. City": "Texto 50 *",
"2.2.7.5. PostalCode": "Texto 20 *",
"2.2.7.6. Region": "Texto 50 ",
"2.2.7.7. Country": "Texto 12 *"
},
"2.2.8. Telephone": "Texto 20 ",
"2.2.9. Fax": "Texto 20 ",
"2.2.10. Email": "Texto 60 ",
"2.2.11. Website": "Texto 60 ",
"2.2.12. SelfBillingIndicator": "Inteiro *"
}






javascript tree number-sequence






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 2:49







bcsantos

















asked Nov 25 '18 at 23:14









bcsantosbcsantos

1,85551619




1,85551619













  • Could you please add a desired result example to the question?

    – Kosh Very
    Nov 25 '18 at 23:19











  • Just did, thank you.

    – bcsantos
    Nov 25 '18 at 23:40






  • 1





    You are better off going back to the original data file. It's tab delimited, where the second element on a line is empty, it has child nodes. Where it contains an asterisk (*), it's a child node with no children. Call the function recursively every time it hits an empty second element, and return when the index decreases. Post what you've tried.

    – RobG
    Nov 26 '18 at 2:10











  • @RobG updated code and output. the asterisk in the source tsv tables is to indicate if a filed is mandatory. the two main cues to look for in nesting objects are the "N/A" string in filed type and the actual number sequence separated by dots. This is making me want to go learn some proper CS and make library for this kind of thing. As it stands, the code above is naive, but sort of works. Input very welcome.

    – bcsantos
    Nov 26 '18 at 2:53











  • As RobG recommended, use a recursive function that greedily reads lines of the same indentation. And to make your code less ugly, build an actual data structure as a return value instead of outputting a JSON text manually.

    – Bergi
    Nov 26 '18 at 7:21



















  • Could you please add a desired result example to the question?

    – Kosh Very
    Nov 25 '18 at 23:19











  • Just did, thank you.

    – bcsantos
    Nov 25 '18 at 23:40






  • 1





    You are better off going back to the original data file. It's tab delimited, where the second element on a line is empty, it has child nodes. Where it contains an asterisk (*), it's a child node with no children. Call the function recursively every time it hits an empty second element, and return when the index decreases. Post what you've tried.

    – RobG
    Nov 26 '18 at 2:10











  • @RobG updated code and output. the asterisk in the source tsv tables is to indicate if a filed is mandatory. the two main cues to look for in nesting objects are the "N/A" string in filed type and the actual number sequence separated by dots. This is making me want to go learn some proper CS and make library for this kind of thing. As it stands, the code above is naive, but sort of works. Input very welcome.

    – bcsantos
    Nov 26 '18 at 2:53











  • As RobG recommended, use a recursive function that greedily reads lines of the same indentation. And to make your code less ugly, build an actual data structure as a return value instead of outputting a JSON text manually.

    – Bergi
    Nov 26 '18 at 7:21

















Could you please add a desired result example to the question?

– Kosh Very
Nov 25 '18 at 23:19





Could you please add a desired result example to the question?

– Kosh Very
Nov 25 '18 at 23:19













Just did, thank you.

– bcsantos
Nov 25 '18 at 23:40





Just did, thank you.

– bcsantos
Nov 25 '18 at 23:40




1




1





You are better off going back to the original data file. It's tab delimited, where the second element on a line is empty, it has child nodes. Where it contains an asterisk (*), it's a child node with no children. Call the function recursively every time it hits an empty second element, and return when the index decreases. Post what you've tried.

– RobG
Nov 26 '18 at 2:10





You are better off going back to the original data file. It's tab delimited, where the second element on a line is empty, it has child nodes. Where it contains an asterisk (*), it's a child node with no children. Call the function recursively every time it hits an empty second element, and return when the index decreases. Post what you've tried.

– RobG
Nov 26 '18 at 2:10













@RobG updated code and output. the asterisk in the source tsv tables is to indicate if a filed is mandatory. the two main cues to look for in nesting objects are the "N/A" string in filed type and the actual number sequence separated by dots. This is making me want to go learn some proper CS and make library for this kind of thing. As it stands, the code above is naive, but sort of works. Input very welcome.

– bcsantos
Nov 26 '18 at 2:53





@RobG updated code and output. the asterisk in the source tsv tables is to indicate if a filed is mandatory. the two main cues to look for in nesting objects are the "N/A" string in filed type and the actual number sequence separated by dots. This is making me want to go learn some proper CS and make library for this kind of thing. As it stands, the code above is naive, but sort of works. Input very welcome.

– bcsantos
Nov 26 '18 at 2:53













As RobG recommended, use a recursive function that greedily reads lines of the same indentation. And to make your code less ugly, build an actual data structure as a return value instead of outputting a JSON text manually.

– Bergi
Nov 26 '18 at 7:21





As RobG recommended, use a recursive function that greedily reads lines of the same indentation. And to make your code less ugly, build an actual data structure as a return value instead of outputting a JSON text manually.

– Bergi
Nov 26 '18 at 7:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472965%2fparse-a-dot-delimited-numerical-hierarchy-index-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472965%2fparse-a-dot-delimited-numerical-hierarchy-index-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)