0

I have parsed JSON object representing response from old OData(v2) service:

"TimesheetDetailsSet": {
    "results": [{
        "TimesheetNumber": "1",
        (...)
            "results": [{
                "__metadata":

So as you can see its a complex object with nested objects and arrays. I use SAP UI5 framework and I need to modify it, so there will be no intermediary arrays called "results". I want "TimesheetDetailsSet" and other entitysets containg "results" to be independent arrays of objects.

I wrote this method:

var fnConvert = function (oData) {

    if (!oData) {
        return oData;
    }

    for (var sKey in oData) {
        if (oData.hasOwnProperty(sKey)) {
            if (sKey == 'results') {
                var oArr = oData[sKey].reduce(function (acc, cur, i) {
                    acc[i] = cur;
                    return acc;
                }, []);

                delete oData[sKey]
                for (var i = 0; i < oArr.length; i++) {
                    oData[i] = oArr[i];
                    fnConvert(oData[i]);
                }

            } else if (typeof oData[sKey] === 'object') {
                fnConvert(oData[sKey]);
            }
        }
    }
    return oData;
};

The only problem is that need entitysets to be arrays not objects.

Result of the method

UPDATE:

I got this:

"TimesheetDetailsSet": {
    "results": [{
        "__metadata": {
            "id": "...",
            "uri": "...",
            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.TimesheetDetails"
        },
        "TimesheetNumber": "1",
        "Username": "DEFAULT_USER",
        "TimesheetKey": "\/Date(1529280000000)\/",
        "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
        "CostObject": "0001",
        "ConstructionSiteSet": {
            "results": [{
                "__metadata": {
                    "id": "...",
                    "uri": "...",
                    "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSite"
                },
                "TimesheetKey": "\/Date(1529280000000)\/",
                "WorkingOnConstrSiteSince": "18.08.2018",
                "Username": "DEFAULT_USER",
                "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
                "ConstructionSiteKey": "00000100-0000-0000-0000-000000000000",
                "ConstructionSiteId": "0001",
                "ConstructionSiteDetailSet": {
                    "results": [{
                        "__metadata": {
                            "id": "...",
                            "uri": "...",
                            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSiteDetail"
                        },

And I want to change it into this:

"TimesheetDetailsSet": [{
        "__metadata": {
            "id": "...",
            "uri": "...",
            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.TimesheetDetails"
        },
        "TimesheetNumber": "1",
        "Username": "DEFAULT_USER",
        "TimesheetKey": "\/Date(1529280000000)\/",
        "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
        "CostObject": "0001",
        "ConstructionSiteSet": [{
                "__metadata": {
                    "id": "...",
                    "uri": "...",
                    "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSite"
                },
                "TimesheetKey": "\/Date(1529280000000)\/",
                "WorkingOnConstrSiteSince": "18.08.2018",
                "Username": "DEFAULT_USER",
                "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
                "ConstructionSiteKey": "00000100-0000-0000-0000-000000000000",
                "ConstructionSiteId": "0001",
                "ConstructionSiteDetailSet": [{
                        "__metadata": {
                            "id": "...",
                            "uri": "...",
                            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSiteDetail"
                        },
Mac Karczewski
  • 133
  • 2
  • 10
  • 2
    *"...parsed JSON object."* Once you've parsed it, it's not JSON anymore. :-) JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jun 19 '18 at 12:00
  • 2
    In your code, what exactly does `oData` refer to? Because what you've quoted at the top of the question is a fragment of something, not valid JSON (or a valid JavaScript object initializer). Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-do-a-runnable-example-with-stack-snippets-how-do-i-do-tha)). – T.J. Crowder Jun 19 '18 at 12:02
  • At least related: [*Access / process (nested) objects, arrays or JSON*](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). – T.J. Crowder Jun 19 '18 at 12:03
  • By JSON object I mean: var data = JSON.parse(rawJsonData). And then: var result = fnConvert(data). – Mac Karczewski Jun 19 '18 at 12:07
  • It's probably easier to generate a new object, than to modify the existing one. You might want to add some valid JSON as well, it's hard to guess what your data looks like with your description – Moritz Roessler Jun 19 '18 at 12:09
  • But to traverse existing object I need to use recursion. – Mac Karczewski Jun 19 '18 at 12:12
  • *is that need entitysets to be arrays not objects.* what's *entitysets* mean? – Liam Jun 19 '18 at 12:19
  • @MacKarczewski - Right. That's just an object, not a "JSON object." – T.J. Crowder Jun 19 '18 at 12:51
  • @MacKarczewski - Your update is still fragmentary. The top-level will start with `[` or `{`, not with `"`. – T.J. Crowder Jun 19 '18 at 12:52
  • @T.J. Crowder - Its only a fragment of huge payload (162 lines when formated). It's a standard JSON file parsed to object. It's used by the framework (SAP UI5) as model (MVC) in binding process. – Mac Karczewski Jun 19 '18 at 12:59
  • @MacKarczewski - Then it's your job to reduce it to a valid [mcve] so people can help you. – T.J. Crowder Jun 19 '18 at 13:01
  • If all you want is an array instead of an object, you can use assign to copy your object to an empty array : `var result = Object.assign([], fnConvert(data))` – J.Loscos Jul 03 '18 at 08:57

0 Answers0