简体   繁体   中英

How to Parse an Array in a JSON object with npm dlv (JavaScript)

I am getting a response from a GET request. I need to parse it and get the value for the key baseEntityCode which would be CPY_COMPANY1 . I am using npm dlv https://www.npmjs.com/package/dlv as I can use the dot-notated path. The JSON has an array and I still haven't found a way to get the key path after I used the example given by npm docs as guidance,

//use string dot notation for keys
delve(obj, 'a.b.c') === 1;

//or use an array key
delve(obj, ['a', 'b', 'c']) === 1;

Here is the JSON response (I've taken out some key values that may be confidential to our company).

{
  "items": [
    {
      "baseEntityAttributes": [
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ABN",
          "readonly": false,
          "index": 0,
          "created": "2020-02-06T23:21:09.414",
          "valueString": "51824753556",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ADDRESS_ADDRESS1",
          "readonly": false,
          "index": 0,
          "created": "2020-02-06T23:17:43.126",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_CPY_NAME",
          "readonly": false,
          "index": 0,
          "created": "removed",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ADDRESS_POSTCODE",
          "readonly": false,
          "index": 0,
          "created": "removed",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ADDRESS_FULL",
          "readonly": false,
          "index": 0,
          "created": "2020-02-06T23:21:01.87",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ADDRESS_COUNTRY",
          "readonly": false,
          "index": 0,
          "created": "removed",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ADDRESS_CITY",
          "readonly": false,
          "index": 0,
          "created": "2020-02-06T23:18:07.324",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        },
        {
          "baseEntityCode": "CPY_COMPANY1",
          "attributeCode": "PRI_ADDRESS_STATE",
          "readonly": false,
          "index": 0,
          "created": "removed",
          "valueString": "removed",
          "weight": 1.0,
          "inferred": false,
          "privacyFlag": false
        }
      ],
      "links": [],
      "questions": [],
      "code": "CPY_COMPANY1",
      "index": 0,
      "created": "removed",
      "id": removed,
      "name": "removed",
      "realm": "removed"
    }
  ],
  "parentCode": "SBE_TEST2",
  "total": 1,
  "returnCount": 1,
  "data_type": "BaseEntity",
  "delete": false,
  "replace": false,
  "msg_type": "DATA_MSG",
  "option": "EXEC"
}

You can use Runkit https://npm.runkit.com/dlv to test your code.

Please let me know if the question is not clear and you need more clarity. Thanks!

It seems like entering the array index number just as you would a key is the way to go. For example

delve(response, 'items.0.baseEntityAttributes.0.baseEntityCode')

gives

CPY_COMPANY1

which is

{
   "items": [
        {
          "baseEntityAttributes": [
            {
              "baseEntityCode": "CPY_COMPANY1", // <--- This value
              "attributeCode": "PRI_ABN",
              "readonly": false,
              "index": 0,
              "created": "2020-02-06T23:21:09.414",
              "valueString": "51824753556",
              "weight": 1.0,
              "inferred": false,
              "privacyFlag": false
            }, ...

Hope this answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM