Test not found value

I don’t really know how to explain my problem. I am querying an api via http request to find out all the properties of a given item. This api returns values for this item and I have to update a database. My problem is that depending on the type of article, not all the properties are present and therefore do not come out of the HTTP request. I still need these properties to appear for the database update. I don’t know how to test the http output to know if the property is present or not in the response and if not, to assign a default value to it.
I’m not sure I’m being very clear in my explanations…

Hey @fporta, we should be able to figure this out. Can you confirm the current response format from your API as shown in n8n as well as the format you want for your database update?

Here are the 2 incriminated worflow components. Let me know if you want some other informations

the Http request response :slight_smile:

[
{
"_links": {
"self": {
"href": "http://pim.coreplighting.net/api/rest/v1/products?page=1&with_count=false&pagination_type=page&limit=10&search=%7B%22identifier%22:%5B%7B%22operator%22:%22%3D%22,%22value%22:%22649000%22,%22locale%22:%22fr_FR%22%7D%5D%7D"
},
"first": {
"href": "http://pim.coreplighting.net/api/rest/v1/products?page=1&with_count=false&pagination_type=page&limit=10&search=%7B%22identifier%22:%5B%7B%22operator%22:%22%3D%22,%22value%22:%22649000%22,%22locale%22:%22fr_FR%22%7D%5D%7D"
}
},
"current_page": 1,
"_embedded": {
"items": [
{
"_links": {
"self": {
"href": "http://pim.coreplighting.net/api/rest/v1/products/649000"
}
},
"identifier": "649000",
"enabled": true,
"family": "IMPELEC",
"categories": [
"deco_crp21",
"local_chambre",
"local_couloir",
"local_cuisine",
"local_salon",
"local_sam",
"spot_crp21",
"uni_indus"
],
"groups": [
],
"parent": null,
"values": {
"elec_ip": [
{
"locale": null,
"scope": null,
"data": "elec_ip_20"
}
],
"fsc_type": [
{
"locale": null,
"scope": null,
"data": "fsc_type_w18_8"
}
],
"id_marque": [
{
"locale": null,
"scope": null,
"data": "id_marque_corep"
}
],
"elec_classe": [
{
"locale": null,
"scope": null,
"data": "elec_classe_1"
}
],
"id_designer": [
{
"locale": null,
"scope": null,
"data": "id_designer_corep"
}
],
"couleur_love": [
{
"locale": null,
"scope": null,
"data": "mark_color_anthracite"
}
],
"energy_class": [
{
"locale": null,
"scope": null,
"data": "energy_class_F"
}
],
"elec_d3e_taxe": [
{
"locale": null,
"scope": null,
"data": "elec_d3e_taxe_led"
}
],
"elec_d3e_taxe2": [
{
"locale": null,
"scope": null,
"data": "elec_d3e_taxe_luminaire_menager_500p_3500m"
}
],
"id_vie_produit": [
{
"locale": null,
"scope": null,
"data": "id_vie_produit_declasse"
}
],
"typo_packaging": [
{
"locale": null,
"scope": null,
"data": "typo_pack_color_box"
}
],
"elec_type_douille": [
{
"locale": null,
"scope": null,
"data": "elec_type_douille_GU10"
}
],
"elec_type_lumiere": [
{
"locale": null,
"scope": null,
"data": "elec_type_lumiere_led"
}
],
"fsc_country_origin": [
{
"locale": null,
"scope": null,
"data": "fsc_country_pologne"
}
],
"depreciation_article": [
{
"locale": null,
"scope": null,
"data": "dep_70"
}
],
"elec_couleur_douille": [
{
"locale": null,
"scope": null,
"data": "elec_couleur_douille_blanc"
}
],
"elec_ampoule_remplacable": [
{
"locale": null,
"scope": null,
"data": "elec_ampoule_remplacable_client"
}
],
"garantie": [
{
"locale": null,
"scope": null,
"data": [
"garantie_2_ans"
]
}
],
"fsc_label": [
{
"locale": null,
"scope": null,
"data": [
"fsc_label_fsc"
]
}
],
"com_matiere": [
{
"locale": null,
"scope": null,
"data": [
"com_matiere_metal"
]
}
],
"fsc_essence": [
{
"locale": null,
"scope": null,
"data": [
"fsc_essence_fagus_sylvatica"
]
}
],
"bois_essence": [
{
"locale": null,
"scope": null,
"data": [
"essence_fagus_sylvatica"
]
}
],
"com_matiere2": [
{
"locale": null,
"scope": null,
"data": [
"com_matiere_wood"
]
}
],
"id_nature_produit": [
{
"locale": null,
"scope": null,
"data": [
"id_nature_produit_spot"
]
}
],
"wood_country_origin": [
{
"locale": null,
"scope": null,
"data": [
"wood_country_pologne"
]
}
],
"elec_d3e_mnttt": [
{
"locale": null,
"scope": null,
"data": [
{
"amount": "0.81",
"currency": "EUR"
}
]
}
],
"pdt_icon": [
{
"locale": null,
"scope": null,
"data": "5/0/c/7/50c7556ce478fdd9c86689239f3d44759cb74748_thumb_16307FB737B8477581C8132BF545C34D.jpg",
"_links": {
"download": {
"href": "http://pim.coreplighting.net/api/rest/v1/media-files/5/0/c/7/50c7556ce478fdd9c86689239f3d44759cb74748_thumb_16307FB737B8477581C8132BF545C34D.jpg/download"
}
}
}
],
"irc": [
{
"locale": null,
"scope": null,
"data": "90.0000"
}
],
"fsc_percent": [
{
"locale": null,
"scope": null,
"data": 100
}
],
"elec_d3e_qte": [
{
"locale": null,
"scope": null,
"data": 2
}
],
"elec_d3e_qte2": [
{
"locale": null,
"scope": null,
"data": 1
}
],
"elec_nbre_pile": [
{
"locale": null,
"scope": null,
"data": 0
}
],
"elec_nbre_douille": [
{
"locale": null,
"scope": null,
"data": "2.0000"
}
],
"led_beam_angle_degres": [
{
"locale": null,
"scope": null,
"data": "37.0000"
}
],
"rated_number_os_switching_cycles": [
{
"locale": null,
"scope": null,
"data": "15000.0000"
}
],
"fsc_fsc": [
{
"locale": null,
"scope": null,
"data": true
}
],
"dimmable": [
{
"locale": null,
"scope": null,
"data": false
}
],
"job_achat_ok": [
{
"locale": null,
"scope": null,
"data": false
}
],
"bois_tropical": [
{
"locale": null,
"scope": null,
"data": false
}
],
"job_photos_ok": [
{
"locale": null,
"scope": null,
"data": false
}
],
"etiq_etiquette": [
{
"locale": null,
"scope": null,
"data": false
}
],
"job_qualite_ok": [
{
"locale": null,
"scope": null,
"data": true
}
],
"job_marketing_ok": [
{
"locale": null,
"scope": null,
"data": false
}
],
"elec_led_integree": [
{
"locale": null,
"scope": null,
"data": false
}
],
"elec_piles_fourni": [
{
"locale": null,
"scope": null,
"data": false
}
],
"job_collection_ok": [
{
"locale": null,
"scope": null,
"data": false
}
],
"elec_certificat_ce": [
{
"locale": null,
"scope": null,
"data": true
}
],
"elec_certificat_cem": [
{
"locale": null,
"scope": null,
"data": true
}
],
"elec_certificat_erp": [
{
"locale": null,
"scope": null,
"data": true
}
],
"elec_certificat_red": [
{
"locale": null,
"scope": null,
"data": false
}
],
"elec_transformateur": [
{
"locale": null,
"scope": null,
"data": false
}
],
"batterie_extractable": [
{
"locale": null,
"scope": null,
"data": false
}
],
"elec_ampoule_incluse": [
{
"locale": null,
"scope": null,
"data": true
}
],
"elec_certificat_rohs": [
{
"locale": null,
"scope": null,
"data": true
}
],
"elec_batterie_fournie": [
{
"locale": null,
"scope": null,
"data": false
}
],
"elec_certificat_reach": [
{
"locale": null,
"scope": null,
"data": true
}
],
"elec_telecommande_fournie": [
{
"locale": null,
"scope": null,
"data": false
}
],
"com_photo": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443/razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/16307FB737B8477581C8132BF545C34D/thumb_16307FB737B8477581C8132BF545C34D.jpg"
}
],
"com_serie": [
{
"locale": null,
"scope": null,
"data": "MOSS"
}
],
"elec_lumen": [
{
"locale": null,
"scope": null,
"data": "350"
}
],
"com_couleur": [
{
"locale": "en_US",
"scope": null,
"data": "anthracite/copper"
},
{
"locale": "fr_FR",
"scope": null,
"data": "anthracite / cuivre"
}
],
"com_libelle": [
{
"locale": "en_US",
"scope": null,
"data": "2-light bi-material painted metal rectangular base and wood certified FSC® spotlight. Adjustable heads. Sold with bulbs."
},
{
"locale": "fr_FR",
"scope": null,
"data": "Spot 2L bi-matière en métal peint base rectangulaire et bois certifié FSC®. Têtes orientables. Vendu avec ampoules. "
}
],
"elec_kelvin": [
{
"locale": null,
"scope": null,
"data": "3000"
}
],
"id_code_erp": [
{
"locale": null,
"scope": null,
"data": "652607"
}
],
"com_wireframe": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443//razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/7080B151774440619DEDA73A1B64E6AB/F_88207.jpg"
}
],
"raz_folder_id": [
{
"locale": null,
"scope": null,
"data": "BD11EC6F378343EE98D7181E7AC26C1B"
}
],
"com_libelle_erp": [
{
"locale": "fr_FR",
"scope": null,
"data": "SPOT.GU10.LED.BOIS.FSC.MOSS.2L"
}
],
"com_nom_produit": [
{
"locale": null,
"scope": null,
"data": "MOSS 2L"
}
],
"erp_code_couleur": [
{
"locale": null,
"scope": null,
"data": "1826"
}
],
"id_gencod_produit": [
{
"locale": null,
"scope": null,
"data": "3188000743671"
}
],
"com_image_ambiance": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443/razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/53E8E412BAC641C894E65FFEBD3D2A99/88207_spot_moss_2L_anthracite_cuivre.jpg"
}
],
"id_famille_produit": [
{
"locale": null,
"scope": null,
"data": "88207"
}
],
"com_image_on_carree": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443/razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/3547830D1D7F4920A4C19DB1EE8A6E09/652607_ON.jpg"
}
],
"rated_starting_time": [
{
"locale": null,
"scope": null,
"data": "0.5"
}
],
"com_image_code_barre": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443/razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/8D67DD261A2240F8850A0F2F9664F3C4/BarCode_3188000743671.png"
}
],
"qual_famille_montage": [
{
"locale": null,
"scope": null,
"data": "FC1GU10"
}
],
"com_libelle_court_web": [
{
"locale": "en_US",
"scope": null,
"data": "Spotlight metal and FSC® certified wood anthracite/copper Corep MOSS 2L L28cm. "
},
{
"locale": "fr_FR",
"scope": null,
"data": "Spot métal et bois certifié FSC® anthracite/cuivre Corep MOSS 2L L28cm. "
}
],
"com_image_ambiance_carree": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443/razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/783290F46B40412A8A53BA70AD0B41A3/652607_LIFESTYLE.jpg"
}
],
"com_image_principale_carree": [
{
"locale": null,
"scope": null,
"data": "https://dam.coreplighting.com:8443/razuna/assets/1/BD11EC6F378343EE98D7181E7AC26C1B/img/A5C3243F91A4401EBB88B42559A32AA1/652607_OFF.jpg"
}
],
"elec_pav_h": [
{
"locale": null,
"scope": null,
"data": {
"amount": "2.5000",
"unit": "CENTIMETER"
}
}
],
"com_hauteur": [
{
"locale": null,
"scope": null,
"data": {
"amount": "14.0000",
"unit": "CENTIMETER"
}
}
],
"com_longueur": [
{
"locale": null,
"scope": null,
"data": {
"amount": "28.0000",
"unit": "CENTIMETER"
}
}
],
"pack_hauteur": [
{
"locale": null,
"scope": null,
"data": {
"amount": "16.5000",
"unit": "CENTIMETER"
}
}
],
"pack_longeur": [
{
"locale": null,
"scope": null,
"data": {
"amount": "32.0000",
"unit": "CENTIMETER"
}
}
],
"com_poids_net": [
{
"locale": null,
"scope": null,
"data": {
"amount": "920.0000",
"unit": "GRAM"
}
}
],
"elec_watt_max": [
{
"locale": null,
"scope": null,
"data": {
"amount": "5.0000",
"unit": "WATT"
}
}
],
"com_poids_brut": [
{
"locale": null,
"scope": null,
"data": {
"amount": "1115.0000",
"unit": "GRAM"
}
}
],
"com_profondeur": [
{
"locale": null,
"scope": null,
"data": {
"amount": "14.0000",
"unit": "CENTIMETER"
}
}
],
"ecoe_plastique": [
{
"locale": null,
"scope": null,
"data": {
"amount": 10,
"unit": "GRAM"
}
}
],
"pack_profondeur": [
{
"locale": null,
"scope": null,
"data": {
"amount": "9.0000",
"unit": "CENTIMETER"
}
}
],
"elec_pav_diametre": [
{
"locale": null,
"scope": null,
"data": {
"amount": "28.0000",
"unit": "CENTIMETER"
}
}
],
"ecoe_papier_carton": [
{
"locale": null,
"scope": null,
"data": {
"amount": 185,
"unit": "GRAM"
}
}
],
"dimension_chips_width": [
{
"locale": null,
"scope": null,
"data": {
"amount": "50.0000",
"unit": "MILLIMETER"
}
}
],
"dimension_chips_lenght": [
{
"locale": null,
"scope": null,
"data": {
"amount": "53.0000",
"unit": "MILLIMETER"
}
}
],
"nominal_lamp_life_time": [
{
"locale": null,
"scope": null,
"data": {
"amount": "25000.0000",
"unit": "HOUR"
}
}
],
"com_libelle_serie": [
{
"locale": "en_US",
"scope": null,
"data": "MOSS"
},
{
"locale": "fr_FR",
"scope": null,
"data": "MOSS"
}
],
"com_libelle_long_web": [
{
"locale": "en_US",
"scope": null,
"data": "2-light bi-material painted metal rectangular base and wood certified FSC® spotlight. Adjustable heads. Sold with bulbs. Color : anthracite/copper. IP20. L28cm - P14cm - H14cm. Class 1. GU10 - 5W - 400lumens - 3000K."
},
{
"locale": "fr_FR",
"scope": null,
"data": "Spot 2L bi-matière en métal peint base rectangulaire et bois certifié FSC®. Têtes orientables. Vendu avec ampoules. Coloris : anthracite/cuivre. IP20. L28cm - P14cm - H14cm. Class 1. GU10 - 5W - 400lumens - 3000K."
}
]
},
"created": "2021-12-20T12:22:46+01:00",
"updated": "2021-12-22T15:39:18+01:00",
"associations": {
"UPSELL": {
"products": [
"472100",
"648900",
"649100",
"649200"
],
"product_models": [
],
"groups": [
]
},
"X_SELL": {
"products": [
"664400",
"667600",
"668700",
"669100",
"736400",
"736500",
"736600",
"736700",
"853200",
"854300",
"854600"
],
"product_models": [
],
"groups": [
]
},
"SUBSTITUTION": {
"products": [
"645600",
"645700",
"697900",
"926400",
"928500"
],
"product_models": [
],
"groups": [
]
}
},
"quantified_associations": {
}
}
]
}
}
]

the Set response where the customs_code is missing because it is not present in the Http request response

"deee2_qte": 1,
"hauteur": "14.0000",
"deee_qte": 2,
"watt_max": "5.0000",
"poids_net": "920.0000",
"poids_brut": "1115.0000",
"cdart": "652607",
"serie": "MOSS",
"cdmarq": "id_marque_corep",
"intitule_pdt": "id_nature_produit_spot",
"prdang": "id_vie_produit_declasse",
"fsc": true,
"taxe_deee2": "elec_d3e_taxe_luminaire_menager_500p_3500m",
"matiere": "com_matiere_metal",
"type_douille": "elec_type_douille_GU10",
"famille": "88207",
"fsc_essence": "fsc_essence_fagus_sylvatica",
"fsc_label": "fsc_label_fsc",
"fsc_type": "fsc_type_w18_8",
"taxe_deee": "elec_d3e_taxe_led"

So the fields you are using in your Set node (such as serie) are the ones that might or might not be included in the data coming your HTTP Request node?

It seems you are using expressions that would set the result to null like {{$node["PIM Get Product"].json["_embedded"]["items"][0]["values"]["com_serie"][0]["data"] || null}}.

This check would, however, fail if a parent property of your nested property isn’t present which I suspect might be the case for you. If so, you could use the optional chaining operator ?. instead. This would let you check if the entire chain is valid without checking each level individually.

So if instead of the aforementioned expression you’d use something like {{$node["PIM Get Product"]?.json?.["_embedded"]?.["items"]?.[0]?.["values"]?.["com_serie"]?.[0]?.["data"] || null}}, you should get the expected null back in case data or any of its properties are missing.

Example Workflow

This is a test I did and forgot to remove. Not being familiar with js development I don’t know this way of testing values. I will try

1 Like

So the idea of your test seems pretty good to me. The approach is called “short-circuit” evaluation and means that all possibilities are separated by the logical OR operator ||. Whichever one is “truthy” first (starting from the left) will then be used (and all others ignored).

I have just done the test and how to say… it works… a big thank you for this lesson of development in js, I was turning in circle since yesterday.
you’re the best !!! :hugs: :hugs: :hugs:

1 Like

Awesome, glad to hear this works and thanks so much for confirming! :slight_smile: