简体   繁体   中英

Nested Json array to objects

We are working on an integration tool, which is generating below JSON which is creating an array of objects instead of objects, our end application doesn't support this array structure, we cannot change the structure in the Integration tool, but we can add javascript which can perform this action. I have added current and expected JSON payloads. Brackets inside dataReq segment need to be removed thanks in advance!

Current Json:

{
    "abc": [
        {
            "abcId": "1",
            "dataReq": [
                {
                    "url": "/api.css.com",
                    "body": {
                        "desc": "Sprint",
                        "na": "Customer Data",
                        "tzone": "America/Los_Angeles"
                    },
                    "method": "PATCH",
                    "refData": "OP"
                },
                [
                    {
                        "url": "/api.css.com/v1",
                        "body": {
                            "day": "Monday",
                            "end": "18:00:00.000Z",
                            "start": "08:00:00.000Z",
                            "hours": "08",
                            "Type": "Normal"
                        },
                        "method": "PATCH",
                        "refData": "timeslotN"
                    },
                    {
                        "url": "/api.css.com/v2",
                        "body": {
                            "DayOfWeek": "Tuesday",
                            "EndTime": "18:00:00.000Z",
                            "StartTime": "08:00:00.000Z",
                            "hours": "09",
                            "Type": "Normal"
                        },
                        "method": "PATCH",
                        "refData": "timeslotN"
                    }
                ],
                {
                    "url": "/api.css.com/v3",
                    "body": {
                        "Name": "Spirit2",
                        "Type": "murchast",
                        "Parent": {
                            "accID": "2"
                        },
                        "street": "CR Steet",
                        "city": "NYK",
                        "state": null,
                        "postal": "9020",
                        "state": null,
                        "country": "LZ",
                    },
                    "method": "PATCH",
                    "refData": "Account"
                },
                {
                    "url": "/api.css.com/v4",
                    "body": {
                        "account": "12321",
                        "state": {
                            "statecode": "311"
                        }
                    },
                    "method": "PATCH",
                    "refData": "acst"
                }
            ]
        }
    ]
}

Expected JSON:

{
    "abc": [
        {
            "abcId": "1",
            "dataReq": [
                {
                    "url": "/api.css.com",
                    "body": {
                        "desc": "Sprint",
                        "na": "Customer Data",
                        "tzone": "America/Los_Angeles"
                    },
                    "method": "PATCH",
                    "refData": "OP"
                },
                {
                        "url": "/api.css.com/v1",
                        "body": {
                            "day": "Monday",
                            "end": "18:00:00.000Z",
                            "start": "08:00:00.000Z",
                            "hours": "08",
                            "Type": "Normal"
                        },
                        "method": "PATCH",
                        "refData": "timeslotN"
                    },
                    {
                        "url": "/api.css.com/v2",
                        "body": {
                            "DayOfWeek": "Tuesday",
                            "EndTime": "18:00:00.000Z",
                            "StartTime": "08:00:00.000Z",
                            "hours": "09",
                            "Type": "Normal"
                        },
                        "method": "PATCH",
                        "refData": "timeslotN"
                    },
                {
                    "url": "/api.css.com/v3",
                    "body": {
                        "Name": "Spirit2",
                        "Type": "murchast",
                        "Parent": {
                            "accID": "2"
                        },
                        "street": "CR Steet",
                        "city": "NYK",
                        "state": null,
                        "postal": "9020",
                        "state": null,
                        "country": "LZ",
                    },
                    "method": "PATCH",
                    "refData": "Account"
                },
                {
                    "url": "/api.css.com/v4",
                    "body": {
                        "account": "12321",
                        "state": {
                            "statecode": "311"
                        }
                    },
                    "method": "PATCH",
                    "refData": "acst"
                }
            ]
        }
    ]
}

Try using Array#flatMap .

You can simply use Array#flatMap to flatten the dataReq array.

data.abc[0].dataReq = data.abc[0].dataReq.flatMap((d) => d);

 const data={abc:[{abcId:"1",dataReq:[{url:"/api.css.com",body:{desc:"Sprint",na:"Customer Data",tzone:"America/Los_Angeles"},method:"PATCH",refData:"OP"},[{url:"/api.css.com/v1",body:{day:"Monday",end:"18:00:00.000Z",start:"08:00:00.000Z",hours:"08",Type:"Normal"},method:"PATCH",refData:"timeslotN"},{url:"/api.css.com/v2",body:{DayOfWeek:"Tuesday",EndTime:"18:00:00.000Z",StartTime:"08:00:00.000Z",hours:"09",Type:"Normal"},method:"PATCH",refData:"timeslotN"}],{url:"/api.css.com/v3",body:{Name:"Spirit2",Type:"murchast",Parent:{accID:"2"},street:"CR Steet",city:"NYK",state:null,postal:"9020",state:null,country:"LZ"},method:"PATCH",refData:"Account"},{url:"/api.css.com/v4",body:{account:"12321",state:{statecode:"311"}},method:"PATCH",refData:"acst"}]}]}; data.abc[0].dataReq = data.abc[0].dataReq.flatMap((d) => d); console.log(JSON.stringify(data))

Here is an generic solution using object-scan .

This solution will pull double nested arrays one level up.

 // const objectScan = require('object-scan'); const data1 = { abc: [ { abcId: '1', dataReq: [ { url: '/api.css.com', body: { desc: 'Sprint', na: 'Customer Data', tzone: 'America/Los_Angeles' }, method: 'PATCH', refData: 'OP' }, [ { url: '/api.css.com/v1', body: { day: 'Monday', end: '18:00:00.000Z', start: '08:00:00.000Z', hours: '08', Type: 'Normal' }, method: 'PATCH', refData: 'timeslotN' }, { url: '/api.css.com/v2', body: { DayOfWeek: 'Tuesday', EndTime: '18:00:00.000Z', StartTime: '08:00:00.000Z', hours: '09', Type: 'Normal' }, method: 'PATCH', refData: 'timeslotN' } ], { url: '/api.css.com/v3', body: { Name: 'Spirit2', Type: 'murchast', Parent: { accID: '2' }, street: 'CR Steet', city: 'NYK', state: null, postal: '9020', state: null, country: 'LZ' }, method: 'PATCH', refData: 'Account' }, { url: '/api.css.com/v4', body: { account: '12321', state: { statecode: '311' } }, method: 'PATCH', refData: 'acst' } ] } ] }; const modify = objectScan(['**.*[*][*]'], { rtn: 'count', filterFn: ({ parent, gparent, property, gproperty }) => { gparent.splice(gproperty + 1, 0, parent[property]); parent.splice(property, 1); if (parent.length === 0) { gparent.splice(gproperty, 1); } } }); console.log(modify(data1)); // => 2 console.log(data1); // => { abc: [ { abcId: '1', dataReq: [ { url: '/api.css.com', body: { desc: 'Sprint', na: 'Customer Data', tzone: 'America/Los_Angeles' }, method: 'PATCH', refData: 'OP' }, { url: '/api.css.com/v1', body: { day: 'Monday', end: '18:00:00.000Z', start: '08:00:00.000Z', hours: '08', Type: 'Normal' }, method: 'PATCH', refData: 'timeslotN' }, { url: '/api.css.com/v2', body: { DayOfWeek: 'Tuesday', EndTime: '18:00:00.000Z', StartTime: '08:00:00.000Z', hours: '09', Type: 'Normal' }, method: 'PATCH', refData: 'timeslotN' }, { url: '/api.css.com/v3', body: { Name: 'Spirit2', Type: 'murchast', Parent: { accID: '2' }, street: 'CR Steet', city: 'NYK', state: null, postal: '9020', country: 'LZ' }, method: 'PATCH', refData: 'Account' }, { url: '/api.css.com/v4', body: { account: '12321', state: { statecode: '311' } }, method: 'PATCH', refData: 'acst' } ] } ] }
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="https://bundle.run/object-scan@15.0.0"></script>

Disclaimer : I'm the author of object-scan

Here is an example that makes it a bit clearer what is going on

 // const objectScan = require('object-scan'); const data2 = { a: { b: [[1, [2, 3]], { c: 4 }] }, d: [{ e: 5 }, 6, [7, 8]] }; const modify = objectScan(['**.*[*][*]'], { rtn: 'count', filterFn: ({ parent, gparent, property, gproperty }) => { gparent.splice(gproperty + 1, 0, parent[property]); parent.splice(property, 1); if (parent.length === 0) { gparent.splice(gproperty, 1); } } }); console.log(modify(data2)); // => 4 console.log(data2); // => { a: { b: [ 1, [ 2, 3 ], { c: 4 } ] }, d: [ { e: 5 }, 6, 7, 8 ] }
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="https://bundle.run/object-scan@15.0.0"></script>

Disclaimer : I'm the author of object-scan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM