Looking for a test script to create entire response body in JSON export (after run automation)

Context: Hey, so I am a non-coder trying to connect to an API, run an automation and export the results to a JSON file. I have figured it all out, but struggling with the test script part. My automation succeeds and has all the details I need in the response body, but when exporting the file, there are blanks instead of responses. I have added text (“array of all properties”) to the test script and saw it worked when exporting.

My question: Now I need to find a way to export the full body of the API output in the test (or similar so that it is included in the JSON export).

Details (like screenshots):

{
    "results": [
        {
            "siren": "XXXXXXXXX",
            "nom_complet": "XXX",
            "nombre_etablissements": 1,
            "nombre_etablissements_ouverts": 1,
            "siege": {
                "activite_principale": "XXXXX",
                "activite_principale_registre_metier": "XXXXXX",
                "adresse": "XXXXX",
                "cedex": null,
                "code_pays_etranger": null,
                "code_postal": "XXXXX",
                "commune": "XXXXX",
                "complement_adresse": null,
                "coordonnees": "XXXXX",
                "date_creation": "XXXXXX",
                "date_debut_activite": "XXXXXX",
                "departement": "XXX",
                "distribution_speciale": XXX,
                "est_siege": true,
                "etat_administratif": "A",
                "geo_adresse": "XXXXX",
                "geo_id": "XXXXX",
                "indice_repetition": null,
                "latitude": "XXXXX",
                "libelle_cedex": null,
                "libelle_commune": "XXXXX",
                "libelle_commune_etranger": null,
                "libelle_pays_etranger": null,
                "libelle_voie": "XXXXX",
                "liste_enseignes": null,
                "liste_finess": null,
                "liste_idcc": null,
                "liste_rge": null,
                "liste_uai": null,
                "longitude": "XXXX",
                "nom_commercial": null,
                "numero_voie": "11",
                "siret": "XXXXX",
                "tranche_effectif_salarie": null,
                "type_voie": "XXXXX"
            },
            "activite_principale": "XXXXX",
            "categorie_entreprise": "XXXXX",
            "date_creation": "XXXX",
            "date_mise_a_jour": "XXXXXX",
            "dirigeants": [
                {
                    "nom": "XXXX",
                    "prenoms": "XXXXX",
                    "annee_de_naissance": "XXXXX",
                    "qualite": "XXXXX",
                    "type_dirigeant": "XXXX"
                }
            ],
            "etat_administratif": "XXX",
            "nature_juridique": "XXX",
            "nom_raison_sociale": "XXXX",
            "section_activite_principale": "XXXX",
            "tranche_effectif_salarie": XXXX,
            "matching_etablissements": [],
            "complements": {
                "collectivite_territoriale": null,
                "convention_collective_renseignee": false,
                "est_entrepreneur_individuel": false,
                "est_entrepreneur_spectacle": false,
                "est_ess": false,
                "est_finess": false,
                "est_rge": false,
                "est_uai": false,
                "identifiant_association": null
            }
        }
    ],
    "total_results": 1,
    "page": 1,
    "per_page": 10,
    "total_pages": 1
}

I’ve already tried: The “array of all properties” here and tried to manipulate it to work with my use case but it did not.

I don’t need all the fields, but for now I am just looking for a command to add to test that will be able to post the entire result there. Later on, I can narrow it down by variable. The variables I will need eventually are:

  • nom_raison_sociale
  • siege.adresse
  • siege.libelle_commune
  • siege.code_postal
  • dirigeants.nom & dirigeants.prenom
  • annee_de_naissance (just the year)
  • activite_principale
  • siren
  • etat_administratif
  • siret

Appreciate any advice on the topic.

Hi @science-meteorologi5

(If I have understood your question) … to get the full body response and specific values you could use the following;

//Set the JSON response as a const variable
const jsonResponse = pm.response.json();

//Output the full response body
console.log("Full response = " + JSON.stringify(jsonResponse));


//Options for outputting specific values
console.log("nom_raison_sociale = " + jsonResponse.results[0].nom_raison_sociale);

console.log("siege.adresse = " + jsonResponse.results[0].siege.adresse);

console.log("siege.libelle_commune = " + jsonResponse.results[0].siege.libelle_commune);

console.log("siege.code_postal = " + jsonResponse.results[0].siege.code_postal);

console.log("dirigeants.nom = " + jsonResponse.results[0].dirigeants[0].nom);

console.log("dirigeants.prenoms = " + jsonResponse.results[0].dirigeants[0].prenoms);

//This one may need a regEx or something if it's just the year from a string value... could you show what format the response should be in?

console.log("annee_de_naissance (just the year) = " + jsonResponse.results[0].dirigeants[0].annee_de_naissance);

console.log("activite_principale = " + jsonResponse.results[0].activite_principale);

console.log("siren = " + jsonResponse.results[0].siren);

console.log("etat_administratif = " + jsonResponse.results[0].siege.etat_administratif);

console.log("siret = " + jsonResponse.results[0].siege.siret);

Example output;

I’m a bit confused as although that page has code within a pm.test block, it doesn’t actually contain any assertions. All its doing is showing you how to target different elements. Arrays within arrays, etc. It doesn’t actually have any tests.

Therefore, my first question to you is what is it you actually want to test?

Could you also explain what do you mean by exporting the file? You mention that you are getting blanks instead, can you provide the code you currently have (which may explain a bit more about what you are trying to do).

Do you want a new object\array, with just the fields that you mentioned?

This isn’t straight forward as some of your elements are within their own arrays. So a single map command isn’t going to cut it. You’ll have to build up your new array one line at a time.

The following are examples of creating a new array with the relevant values, or a flat object with the new values. (I would probably go with the flat object).

I’ve included a few of the elements. See @w4dd325’s post for how to target the rest.

const response = pm.response.json().data

// create new array object with relevant values

let newArray = [];

newArray.push(
    {
        nom_raison_sociale: `${response.results[0].nom_raison_sociale}`,
        adresse: `${response.results[0].siege.adresse}`,
        code_postal: `${response.results[0].siege.code_postal}`,
        nom: `${response.results[0].dirigeants[0].nom}`,
        prenoms: `${response.results[0].dirigeants[0].prenoms}`
    }
);

console.log(newArray);

// Create flat object with relevant values

let newObj = {
    nom_raison_sociale: `${response.results[0].nom_raison_sociale}`,
    adresse: `${response.results[0].siege.adresse}`,
    code_postal: `${response.results[0].siege.code_postal}`,
    nom: `${response.results[0].dirigeants[0].nom}`,
    prenoms: `${response.results[0].dirigeants[0].prenoms}`
}

console.log(newObj);

image

Still not sure what you want to test though.