Extract data into globalvariable

I want to extract the value 0dad692e-e256-4030-95b7-bda85d0ab4d8 from this response into a global variable, how do i do that?

[
{
“eventTime”: “2023-03-02T08:44:01.489Z”,
“recordTime”: “2023-03-02T07:44:02.168Z”,
“eventTimeZoneOffset”: “+01:00”,
“baseExtension”: null,
“otherAttributes”: {},
“bizStep”: “urn:epcglobal:cbv:bizstep:packing”,
“readPoint”: null,
“parentID”: “00357072944000271034”,
“childEPCs”: {
“epc”: [
{
“value”: “0104030700134336211223039122485001”
}
]
},
“action”: “ADD”,
“disposition”: “urn:epcglobal:cbv:disp:in_progress”,
“bizLocation”: {
“id”: “urn:epc:id:sgln:579000205241…1”,
“extension”: null,
“any”:
},
“extension”: null,
“any”: ,
“eventType”: “AGGREGATION_EVENT”,
“anyFcc”: [
{
“children”: ,
“name”: “urn:atos:extension:xsd:market”,
“value”: “DE”,
“attributes”: {},
“type”: “STRING”
},
{
“children”: ,
“name”: “urn:atos:extension:xsd:bizCountry”,
“value”: “DK”,
“attributes”: {},
“type”: “STRING”
},
{
“children”: ,
“name”: “urn:saga:extension:xsd:originalDocumentId”,
“value”: “0dad692e-e256-4030-95b7-bda85d0ab4d8”,
“attributes”: {},
“type”: “STRING”
}
],
“bizLocExFcc”: ,
“bizLocAnyFcc”: ,
“readPointExFcc”: ,
“readPointAnyFcc”: ,
“errorDeclarationAnyFcc”: ,
“eventId”: “00086f80-b8ce-11ed-99dd-4f3689df236f”,
“partitionKey”: 1677740400206,
“numberOfChildNodes”: 0,
“exFcc”:
}
]

Why a global variable, are you really sure you want this value available to all workspaces and collections in Postman? I would recommend a Collection or Environment variable.

This is how to directly target the element.

let response = pm.response.json()
console.log(response[0].anyFcc[2].value);

Your response is an array, although it only appears to have one element.

Array indexes start at zero, so its response[0].

Your field is under the anyFcc element, which is also an array.

"anyFcc": [
    {
        "children": [],
        "name": "urn:atos:extension:xsd:market",
        "value": "DE",
        "attributes": {},
        "type": "STRING"
    },
    {
        "children": [],
        "name": "urn:atos:extension:xsd:bizCountry",
        "value": "DK",
        "attributes": {},
        "type": "STRING"
    },
    {
        "children": [],
        "name": "urn:saga:extension:xsd:originalDocumentId",
        "value": "0dad692e-e256-4030-95b7-bda85d0ab4d8",
        "attributes": {},
        "type": "STRING"
    }
],

It’s the the third element in that array in the value field, so its ‘anyFcc[2].value’.

This may be brittle, and will fail if the children aren’t returned consistently.

You may need to search for “DocumentId” in the name instead.

One way to do this.

console.log(response[0].anyFcc.find(element => {return element.name.includes('DocumentId')}).value);

How to assign to variables?

This is a core Postman concept. I would recommend you go through the basics in the Learning Centre first.

image

In particular the ‘APIs 101’ and ‘Testing and Automation courses’.

You can read more here about variables.

Using variables | Postman Learning Center

Please remember to use the preformatted text option in the editor when posting code or JSON responses, as its stops all the text being aligned to the left.

Thank you, that was exactly what i needed.