0

I am an old-school C++ programmer - trying to get to grips with Postman, JSON, REST APIs, etc, and struggling..

I am trying to write a Postman test to visualize some JSON response data - which I would like to show in a table format with some merged key names as column headings.

The problem is that the number of data items in a response can vary and the key names can also vary - depending on the input parameters.

So say, for the following JSON which has two data items:

{
    "data": [
        {
            "input": [
                {
                    "value": "ABC",
                    "identifierType": "a1"
                }
            ],
            "output": [
                {
                    "value": "BT",
                    "identifierType": "b1",
                    "name": "BT GROUP",
                    "status": "Active",
                    "classification": "Ordinary"
                }
            ]
        },
        {
            "input": [
                {
                    "value": "BCD",
                    "identifierType": "a1"
                }
            ],
            "output": [
                {
                    "value": "EFG",
                    "identifierType": "b1",
                    "name": "SIEMENS",
                    "status": "Active",
                    "classification": "Ordinary"
                }
            ]
        }
    ]
}

I want to end up with a collection containing column headings that looks something like this: [“Input value”, “Input identifierType”,“Output value”,“Output identifierType”,“Output name”,“Output status”,“Output classification”]

I can get part of the way with something like the following:

function parseData(response, host) {
    const results = response.map(elem => (
        [
            elem.input[0].value,
            elem.input[0].identifierType,
            elem.output[0].value,
            elem.output[0].identifierType,
            elem.output[0].name,
            elem.output[0].status,
            elem.output[0].classification,
        ] 
    ));
    const headers = [];
    for (const key in response[0].input[0]){
        headers.push(`Input ${key}`)
    }
    for (const key in response[0].output[0]){
        headers.push(`Output ${key}`)
    }

    return [results, headers]
}

This gives me the desired headers: [“Input value”, “Input identifierType”,“Output value”,“Output identifierType”,“Output name”,“Output status”,“Output classification”]

However, I want to make this more general i.e. not have to specify input[0] and output[0] in the for loops - as these key names could differ for different query responses.

I did ask this question on the Postman forums and someone helpfully provided a code snippet that allows me to extract the 'input' and 'output' names.

for (const _outer in response[0]) {
    for (const _inner in _outer) {
        for (const key in _inner) {
            headers.push(`${_outer} ${key}`)
        }
    }
}

But that only gives me: ["input 0", "input 0", "input 0", "input 0", "input 0", "output 0' …] for the headers. For some reason I cannot access the inner keys such as value, name, identifierType, status etc

Can someone please suggest where the above is going wrong / how to get what I am after? Thanks.

yaakov
  • 4,568
  • 4
  • 27
  • 51
UNLseg
  • 1
  • 2
  • Check out: https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys – JonSG Jan 10 '23 at 15:06
  • In the example input, all of the arrays have one element. Will that always be the case? How do you want the columns to look for lengths > 1? – danh Jan 10 '23 at 15:17
  • And if the key names might differ, how will you know which key should be used as the "input" array and which for the "output" array? – danh Jan 10 '23 at 15:20
  • Thanks for the replies. @danh - the input and output nodes actually are just examples of the response I can expect, these nodes could be named differently - depending on the original request query. In this instance, the input node is just repeating the input parameter sent in the original request query. – UNLseg Jan 10 '23 at 15:48
  • Okay. Glad you got it working. – danh Jan 10 '23 at 15:54
  • I should also mention, that I did not get any emails notifying me of your comments - hence why I posted my answer before responding to your comments.. – UNLseg Jan 10 '23 at 15:54

1 Answers1

0

Ok - I managed to get an answer on the Postman Forums - thanks.

the solution is:

for (const outerkey in response[0]){
    for (const innerkey in response[0][`${outerkey}`][0]){
        headers.push(`${outerkey} ${innerkey}`)
    }
}

and gave me: "input value", "input identifierType", "output value", "output identifierType", "output name", …]

UPDATE: The data items extraction was still hardcoded with key names (e.g. elem.input[0].identifierType and so), so I replaced that also to give the following solution:

function parseData(response, host) {
    const results = response.map(function(val, index){
        temp = [];
        for (const outerkey in val){
            for (const innerkey in val[`${outerkey}`][0]){
                temp.push(val[`${outerkey}`][0][`${innerkey}`]);
            }
        }
        return temp
    }
    );
    
    const headers = [];

    for (const outerkey in response[0]){
        for (const innerkey in response[0][`${outerkey}`][0]){
            headers.push(`${outerkey} ${innerkey}`)
        }
    }
    return [results, headers]
}
UNLseg
  • 1
  • 2
  • I also changed the data extraction code which previously hardcoded key names to something more generic: – UNLseg Jan 11 '23 at 12:18