0

Comparing an HTTP Post body in node.js to a JSON file, and if there's a match provide the details from the JSON file.

I've tried a couple of different functions, and not sure if my JSON file isn't formatted appropriately for what I need, or if there's something else that I'm missing.

I've attempted to use this function to do the checking:

function checkForValue(json, value) {
  for (lic_key in json) {
      if (typeof (json[lic_key]) === "object") {
          return checkForValue(json[lic_key], value);
      } else if (json[lic_key] === value) {
          //new requirement - return whole object. 
          console.log("here's the key:")
          console.log(json);
          return json;

      }
  }
  return false;
}
function checkForValue2(licenses, input_key) {

  licenses.forEach(function(lic_key) {
    console.log(lic_key.lic_key);
    if (lic_key.lic_key == input_key){
      console.log("We have a match!")
      return lic_key;
    };
    //console.log(lic_key)
  });
}

Here's my JSON file too.

[
  {"lic_key": "5d6d0916c810c639cced1da7",
    "meta":
      {
        "exp_date": "09/23/2019"},
        "organization": "Westar"
      },
    {
      "lic_key": "5d6d091601edbefc2c7f5af6",
      "meta":
      {
        "exp_date": "09/23/2019"}
      },
    {
      "lic_key": "5d6d0916f89d16775a54473d",
      "meta":
      {
        "exp_date": "09/23/2019"}
      },
    {
      "lic_key": "5d6d0916668f82f5b3e1a667",
      "meta":
      {
        "exp_date": "09/23/2019"}
      },
    {
      "lic_key": "5d6d0916012065c066976e5e",
      "meta":
      {
        "exp_date": "09/23/2019"}
      }
  ]

I'd like for the function to return the object where the lic_key matches the second variable passed into the functions. The first variable is the JSON object that I'm loading from a file.

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
Kasey
  • 307
  • 1
  • 8
  • Possible duplicate of https://stackoverflow.com/questions/46302169/compare-two-object-with-deep-comparision-or-with-json-stringify / https://stackoverflow.com/questions/26049303/how-to-compare-two-json-have-the-same-properties-without-order / https://stackoverflow.com/questions/15376185/is-it-fine-to-use-json-stringify-for-deep-comparisons-and-cloning – Constantin Groß Oct 01 '19 at 13:55

1 Answers1

0

You can use Array.filter. Refer - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

const jsonArray = [{
    "lic_key": "5d6d0916c810c639cced1da7",
    "meta": {
      "exp_date": "09/23/2019"
    },
    "organization": "Westar"
  },
  {
    "lic_key": "5d6d091601edbefc2c7f5af6",
    "meta": {
      "exp_date": "09/23/2019"
    }
  },
  {
    "lic_key": "5d6d0916f89d16775a54473d",
    "meta": {
      "exp_date": "09/23/2019"
    }
  },
  {
    "lic_key": "5d6d0916668f82f5b3e1a667",
    "meta": {
      "exp_date": "09/23/2019"
    }
  },
  {
    "lic_key": "5d6d0916012065c066976e5e",
    "meta": {
      "exp_date": "09/23/2019"
    }
  }
];

let findData = (json, value) => {
  const filtered = json.filter(j => j.lic_key === value);

  return (filtered && filtered[0]) || {};
};

console.log(findData(jsonArray, '5d6d0916012065c066976e5e'));
Aditya Bhave
  • 998
  • 1
  • 5
  • 10