1

I have a set of JSON objects having the wsDestAddress key, so how do I traverse the JSON objects or do a wild search for wsDestAddress key in present JSON objects to find the key is present or not and returning null or ""?

JSON Object #1

{
  "gfutd": {
    "wsRequestData": {
      "wsDestAddress": ""
    }
  }
}

JSON Object #2

{
  "igftd": {
    "wsRequestData": {
      "wsDestAddress": ""
    }
  }
}

JSON Object #3

{
  "y7igfutd": {
    "wsResponseData": {
      "wsDestAddress": ""
    }
  }
}

JSON Object #4

{
  "y7igptdf": {
    "wsRequestData": {
      "returnAddress": {
        "wsDestAddress": ""
      }
    }
  }
}

I know this code works fine

if (y7igfutd.wsRequestData.wsDestAddress == "" ||
  igftd.wsRequestData.wsDestAddress == "" ||
  y7igfutd.wsResponseData.wsDestAddress == "" ||
  y7igfutd.wsRequestData.returnAddress.wsDestAddress == "") {
  return "result"
}

But I want to do a wild search for wsDestAddress as the JSON keys search.

2 Answers 2

1

Here is an answer using object-scan. Your requirements were not entirely clear, but I'm sure you can easily adjust the below code to meet your needs.

// const objectScan = require('object-scan');

const data1 = { gfutd: { wsRequestData: { wsDestAddress: '' } } };
const data2 = { igftd: { wsRequestData: { wsDestAddress: '' } } };
const data3 = { y7igfutd: { wsResponseData: { wsDestAddress: '' } } };
const data4 = { y7igptdf: { wsRequestData: { returnAddress: { wsDestAddress: '' } } } };
const data5 = { y7igptdf: { wsRequestData: { returnAddress: { other: '' } } } };

const search = objectScan(['**.wsDestAddress'], {
  filterFn: ({ value }) => value === '',
  rtn: 'bool',
  abort: true
});

console.log(search(data1));
// => true
console.log(search(data2));
// => true
console.log(search(data3));
// => true
console.log(search(data4));
// => true
console.log(search(data5));
// => false
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

Disclaimer: I'm the author of object-scan

Sign up to request clarification or add additional context in comments.

Comments

1

You can find the first item that has a "wsDestAddress" value of "" via:

const data = [
  { "y7igfutd" : { "wsResponseData" : { "wsDestAddress" : "" }}},
  { "igftd"    : { "wsRequestData"  : { "wsDestAddress" : "" }}},
  { "y7igfutd" : { "wsResponseData" : { "wsDestAddress" : "" }}},
  { "y7igptdf" : { "wsRequestData"  : { "returnAddress" : { "wsDestAddress" : "" }}}}
];

 // Adapted from: https://stackoverflow.com/a/40604638/1762224
const findValue = (object, key) => {
  let value;
  Object.keys(object).some(k => {
    if (k === key) {
      value = object[k];
      return true;
    }
    if (object[k] && typeof object[k] === 'object') {
      value = findValue(object[k], key);
      return value !== undefined;
    }
  });
  return value;
};

const oneMatches = (arr, key, value) =>
  arr.some(item => findValue(item, key) === value);
  
const allMatches = (arr, key, value) =>
  arr.every(item => findValue(item, key) === value);
  
console.log(oneMatches(data, 'wsDestAddress', '')); // Some  = true
console.log(allMatches(data, 'wsDestAddress', '')); // Every = true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.