2

I have json data like this:

{
    "s:Envelope": {
        "s:Body": {
            "ExecuteSnapshotRequestsResponse": {
                "ExecuteSnapshotRequestsResult": {
                    "a:SnapshotResponseItemBase": [
                        {
                            "a:AdditionalDetails": {},
                            "a:Error": {},
                            "a:Tag": {
                                "_text": "0~##~vod~##~PI,DY,MV"
                            },
                            "a:Instrument": {
                                "_text": "vod"
                            },
                            "a:InstrumentDisplay": {},
                            "a:Currency": {
                                "_text": "£ "
                            },
                            "a:DataTypeValues": {
                                "a:DataTypeResponseValueBase": [
                                    {
                                        "a:DataType": {
                                            "_text": "PI"
                                        },
                                        "a:DataTypeDisplayName": {
                                            "_text": "PRICE INDEX"
                                        },
                                        "a:Value": {
                                            "_text": "1238.4"
                                        }
                                    },
                                    {
                                        "a:DataType": {
                                            "_text": "DY"
                                        },
                                        "a:DataTypeDisplayName": {
                                            "_text": "DIVIDEND YIELD"
                                        },
                                        "a:Value": {
                                            "_text": "9.25"
                                        }
                                    },
                                    {
                                        "a:DataType": {
                                            "_text": "MV"
                                        },
                                        "a:DataTypeDisplayName": {
                                            "_text": "MARKET VALUE"
                                        },
                                        "a:Value": {
                                            "_text": "38823.49"
                                        }
                                    }
                                ]
                            },
                            "a:Date": {
                                "_text": "2019-04-17T00:00:00"
                            }
                        },
                        {
                            "a:AdditionalDetails": {},
                            "a:Error": {},
                            "a:Tag": {
                                "_text": "0~##~@AAPL~##~PI,DY,MV"
                            },
                            "a:Instrument": {
                                "_text": "@AAPL"
                            },
                            "a:InstrumentDisplay": {},
                            "a:Currency": {
                                "_text": "U$"
                            },
                            "a:DataTypeValues": {
                                "a:DataTypeResponseValueBase": [
                                    {
                                        "a:DataType": {
                                            "_text": "PI"
                                        },
                                        "a:DataTypeDisplayName": {
                                            "_text": "PRICE INDEX"
                                        },
                                        "a:Value": {
                                            "_text": "39566.2"
                                        }
                                    },
                                    {
                                        "a:DataType": {
                                            "_text": "DY"
                                        },
                                        "a:DataTypeDisplayName": {
                                            "_text": "DIVIDEND YIELD"
                                        },
                                        "a:Value": {
                                            "_text": "1.44"
                                        }
                                    },
                                    {
                                        "a:DataType": {
                                            "_text": "MV"
                                        },
                                        "a:DataTypeDisplayName": {
                                            "_text": "MARKET VALUE"
                                        },
                                        "a:Value": {
                                            "_text": "957814.5"
                                        }
                                    }
                                ]
                            },
                            "a:Date": {
                                "_text": "2019-04-17T00:00:00"
                            }
                        }
                    ]
                }
            }
        }
    }
}

I would like to get output like this:

[['vod','1238.4','9.25','38823.49'],['@AAPL','39566.2','1.44','957814.5']]

I tried this but I am getting output in single array only:

let arr = [];

totalRows.forEach(v => {
    arr.push(v["a:Instrument"]["_text"]);
    v["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map((e, i) => {
        arr.push(e["a:Value"]["_text"]);
    });
});

but I want multiple array for each a:SnapshotResponseItemBase and get a:Instrument and a:Value

Update:

I have fixed it like this but can this be optimized ?

let arr = [];
for (var i = 0; i < totalRowsCount; i++) {
    arr.push(totalRows[i]["a:Instrument"]["_text"]);
    for (var j = 0; j < totalRows[i]["a:DataTypeValues"]["a:DataTypeResponseValueBase"].length; j++) {
        arr.push(totalRows[i]["a:DataTypeValues"]["a:DataTypeResponseValueBase"][j]["a:Value"]["_text"]);
    }
}
3
  • As an aside, why do you want it to be an array of arrays instead of a dictionary of arrays where the key is vod or @AAPL and the values are the numbers. Commented Apr 25, 2019 at 3:14
  • arr.push(...v["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map( e => e["a:Value"]["_text"])) Commented Apr 25, 2019 at 3:18
  • @stackErr that's how existing application understands the data Commented Apr 25, 2019 at 3:33

2 Answers 2

2
const arr = totalRows
  .map(el => [
    el['a:Instrument']._text,
    ...el['a:DataTypeValues']['a:DataTypeResponseValueBase']
      .map(e => e['a:Value']._text)
   ]);
Sign up to request clarification or add additional context in comments.

Comments

2

Try this out mate

arr = totalRows.map(element => {
    let one = element["a:Instrument"]["_text"];
    let two = element["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map(subArrayElement => {
        return subArrayElement["a:Value"]["_text"];
    })
    return [...one, ...two];
})
console.log(arr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.