0

Let's say I have the following JSON:

{
    "doc": {
        "left_margin": 2,
        "right_margin": 2,
        "receipt": {
            "emptyLine": "",
            "id": 133,
            "name": "depositOnline",
            "line": [{
                "dataItem": [{
                    "ref": "depositAmount",
                    "label": "receipt.deposit.amountLabel"
                }, {
                    "ref": "currency"
                }]
            }, {
                "dataItem": [{
                    "ref": "depositCustomerName",
                    "label": "transfer.confirm.name"
                }, {
                    "ref": "currency"
                }]
            }],
            "dataItem": [{
                "text": "receipt.deposit.explanationDone"
            }, {
                "ref": "depositNumber",
                "label": "receipt.deposit.numberLabel.deposit"
            }, {
                "ref": "depositResolve"
            }],
            "portion": [{
                "id": 1
            }, {
                "id": 4
            }, {
                "id": 5
            }, {
                "id": 2
            }]
        },
        "portion": [{
            "id": 1,
            "lineFiller": {
                "char": "-"
            },
            "align": "center",
            "name": "header",
            "line": {
                "dataItem": [{
                    "ref": "date"
                }, {
                    "ref": "time"
                }, {
                    "ref": "trace",
                    "label": "traceLabel"
                }]
            },
            "dataItem": [{
                "ref": "bankName",
                "print_type": "bold"
            }, {
                "ref": "terminalName"
            }, {
                "ref": "terminalId"
            }, {
                "ref": "receiptTitle"
            }]
        }, {
            "id": 2,
            "lineFiller": {
                "char": "-"
            },
            "name": "footer",
            "dataItem": {
                "ref": "motto"
            }
        }, {
            "id": 4,
            "name": "cardNumber",
            "dataItem": {
                "ref": "cardNumber",
                "label": "cardNumberLabel"
            }
        }, {
            "id": 5,
            "name": "rrnNumber",
            "dataItem": {
                "ref": "rrn",
                "label": "rrnLabel"
            }
        }]
    }
}

I want to find all the portion keys in this JSON and replace their bodies in receipt keys with portion definitions. Is there a function for something like this? I tried some solutions myself, I wrote a recursive method but it did not work. Could anybody give me an idea on how to do it?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Setareh
  • 3
  • 1
  • 1
    Show us the output you want to get and it's gonna be easier for us to help you – lucas Mar 13 '20 at 12:56
  • You can try my JSON iterator https://github.com/eltomjan/ETEhomeTools/blob/master/HTM_HTA/JSON_Iterator_IIFE.js to traverse your JSON non-recursive - quite complicated usecase for example here https://stackoverflow.com/questions/58008420/how-to-obtain-a-master-structure-for-a-json-file#58013072 It has also 2 predefined iterators DepthFirst and BreadthFirst. – Jan Mar 13 '20 at 13:04
  • If the JSON above is `data`, the code `data.doc.receipt.portion.map(keys=>data.doc.portion.filter(def=> def.id==keys.id)[0])` should do what you need.. – loxxy Mar 13 '20 at 13:13

1 Answers1

0

You would have to map the items in data.doc.receipt.portion to their corresponding data.doc.portion value by finding the matching id field.

You can delete the portion key from data.doc after you are finished.

let data = getData();
data.doc.receipt.portion = data.doc.receipt.portion.map(p => {
  return data.doc.portion.find(p1 => p.id === p1.id);
});
delete data.doc.portion;

console.log(data)

function getData() {
  return {
    "doc": {
      "left_margin": 2,
      "right_margin": 2,
      "receipt": {
        "emptyLine": "",
        "id": 133,
        "name": "depositOnline",
        "line": [{
          "dataItem": [{
            "ref": "depositAmount",
            "label": "receipt.deposit.amountLabel"
          }, {
            "ref": "currency"
          }]
        }, {
          "dataItem": [{
            "ref": "depositCustomerName",
            "label": "transfer.confirm.name"
          }, {
            "ref": "currency"
          }]
        }],
        "dataItem": [{
          "text": "receipt.deposit.explanationDone"
        }, {
          "ref": "depositNumber",
          "label": "receipt.deposit.numberLabel.deposit"
        }, {
          "ref": "depositResolve"
        }],
        "portion": [{
          "id": 1
        }, {
          "id": 4
        }, {
          "id": 5
        }, {
          "id": 2
        }]
      },
      "portion": [{
        "id": 1,
        "lineFiller": {
          "char": "-"
        },
        "align": "center",
        "name": "header",
        "line": {
          "dataItem": [{
            "ref": "date"
          }, {
            "ref": "time"
          }, {
            "ref": "trace",
            "label": "traceLabel"
          }]
        },
        "dataItem": [{
          "ref": "bankName",
          "print_type": "bold"
        }, {
          "ref": "terminalName"
        }, {
          "ref": "terminalId"
        }, {
          "ref": "receiptTitle"
        }]
      }, {
        "id": 2,
        "lineFiller": {
          "char": "-"
        },
        "name": "footer",
        "dataItem": {
          "ref": "motto"
        }
      }, {
        "id": 4,
        "name": "cardNumber",
        "dataItem": {
          "ref": "cardNumber",
          "label": "cardNumberLabel"
        }
      }, {
        "id": 5,
        "name": "rrnNumber",
        "dataItem": {
          "ref": "rrn",
          "label": "rrnLabel"
        }
      }]
    }
  }
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132