JSON verify that some node ha a value

47 views Asked by At

I have this JSON response:

{"modelpageStartData":{"WERKS":"2371","NAME1":"GAYLORD STORE","MATNR":"5405873","BISMT":"","EAN11":"","MAKTX":"LØPESKO ADIDAS","MEINS":"","BSTME":""},"modeltabReturnData":[],"modeltabArticleData":[{"DETAIL_TYPE":"MATNR","DETAIL_LABEL":"Varenummer i SAP","DETAIL_VALUE":"5405873","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"EAN","DETAIL_LABEL":"GTIN","DETAIL_VALUE":"4051936459253","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"SUPPLIER","DETAIL_LABEL":"Leverandør","DETAIL_VALUE":"G63942","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"SORTIMENT","DETAIL_LABEL":"Sortiment","DETAIL_VALUE":"P","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"ORIGINAL_PRICE","DETAIL_LABEL":"Ordinær pris","DETAIL_VALUE":"799.00 NOK","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"BSTME","DETAIL_LABEL":"Bestillingsenhet","DETAIL_VALUE":"","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"UNITS","DETAIL_LABEL":"","DETAIL_VALUE":"","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"MATNR","DETAIL_LABEL":"Varenummer i SAP","DETAIL_VALUE":"5405873","DETAIL_FILTER":"2"},{"DETAIL_TYPE":"LAST_ORDER","DETAIL_LABEL":"Siste bestilling","DETAIL_VALUE":"00.00.0000","DETAIL_FILTER":"2"},{"DETAIL_TYPE":"MENGE","DETAIL_LABEL":"Antall på vei inn","DETAIL_VALUE":"0.000 ","DETAIL_FILTER":"2"},{"DETAIL_TYPE":"LAST_COUNT","DETAIL_LABEL":"Siste lagertelling","DETAIL_VALUE":"00.00.0000","DETAIL_FILTER":"2"}]}

I want to assure that the DETAIL_TYPE":"ORIGINAL_PRICE -> DETAIL_VALUE actually has a value (now it is 799.00 NOK). But have do I write a JSON expression that verifies that this node has actuals content on this form?

1

There are 1 answers

0
Quentin On

There's no such thing as a JSON expression. It's a data format, not a programming language. If you want to test anything about it you have to start by picking a programming language.

The basic steps are the same in any language:

  1. Parse the JSON
  2. Find the object with the DETAIL_TYPE you want
  3. Check that the property you care about is defined on it

Here's how you would do it in Perl.

#!/usr/bin/env perl
use v5.16;
use strict;
use warnings;
use JSON;

my $json = q!{"modelpageStartData":{"WERKS":"2371","NAME1":"GAYLORD STORE","MATNR":"5405873","BISMT":"","EAN11":"","MAKTX":"LØPESKO ADIDAS","MEINS":"","BSTME":""},"modeltabReturnData":[],"modeltabArticleData":[{"DETAIL_TYPE":"MATNR","DETAIL_LABEL":"Varenummer i SAP","DETAIL_VALUE":"5405873","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"EAN","DETAIL_LABEL":"GTIN","DETAIL_VALUE":"4051936459253","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"SUPPLIER","DETAIL_LABEL":"Leverandør","DETAIL_VALUE":"G63942","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"SORTIMENT","DETAIL_LABEL":"Sortiment","DETAIL_VALUE":"P","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"ORIGINAL_PRICE","DETAIL_LABEL":"Ordinær pris","DETAIL_VALUE":"799.00 NOK","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"BSTME","DETAIL_LABEL":"Bestillingsenhet","DETAIL_VALUE":"","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"UNITS","DETAIL_LABEL":"","DETAIL_VALUE":"","DETAIL_FILTER":"1"},{"DETAIL_TYPE":"MATNR","DETAIL_LABEL":"Varenummer i SAP","DETAIL_VALUE":"5405873","DETAIL_FILTER":"2"},{"DETAIL_TYPE":"LAST_ORDER","DETAIL_LABEL":"Siste bestilling","DETAIL_VALUE":"00.00.0000","DETAIL_FILTER":"2"},{"DETAIL_TYPE":"MENGE","DETAIL_LABEL":"Antall på vei inn","DETAIL_VALUE":"0.000 ","DETAIL_FILTER":"2"},{"DETAIL_TYPE":"LAST_COUNT","DETAIL_LABEL":"Siste lagertelling","DETAIL_VALUE":"00.00.0000","DETAIL_FILTER":"2"}]}!;



# 1. Parse the JSON
my $data = JSON::from_json($json);

# 2. Find the object with the DETAIL_TYPE you want
my ($original_price) = grep { $_->{DETAIL_TYPE} eq "ORIGINAL_PRICE" } @{$data->{modeltabArticleData}};

# 3. Check that the property you care about is defined on it
if (defined $original_price->{DETAIL_VALUE}) {
    say "Defined";
} else {
    say "Not defined";
}

This assumes that each DETAIL_TYPE is unique, and that there will be one for ORIGINAL_PRICE. You'd need additional tests if either of those assumptions are false.