0

I have the JSON file:

{
  "src": "Dvc",
   "rte": {
    "src": "dvc"
},
"msgTyp": "dvc.act",
"srcTyp": "dvc.act",
"cntxt": "",
"obj": {
"clbcks": [
  {
    "type": "",
    "title": "",
    "fields": [
      {
        "src": "label.PNG",
        "id": "Label",
      },
      {
        "id": "MSG",
        "text": "APPROVED",
        "type": "label"
      },
      {
        "id": "amt",
        "text": {
          "text": "{0:currency}",
          "substitute": {
            "data": [
              "$requestedAmount"
            ]
          }
        },
        "type": "lbl"
      },
   ],
  }
]

I am trying to reach the $requestedAmount. This is my code:

I tried looping through the JSON file to check if the "data" existed.This is the code that I am using:

var order = obj.clbcks;
for ( i in order ) 
{
    if ( order[i].hasOwnProperty( 'data' ) )
      {
          //do something                         
      }
 }

It's throwing an error. Can someone please help. Thanks.

Adrija
  • 99
  • 2
  • 11
  • Do you need a loop? How does `obj.clbcks[0].fields[2].text.substitute.data[0]` work? – ggorlen Sep 03 '19 at 23:04
  • See also [How can I access and process nested objects, arrays, or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json/68460317#68460317) – ggorlen Jul 30 '22 at 23:01

2 Answers2

0

First off, your JSON is invalid, you're missing out on a final bracket (your indention was wrong).

{
  "src": "Dvc",
   "rte": {
    "src": "dvc"
  },

  "msgTyp": "dvc.act",
  "srcTyp": "dvc.act",
  "cntxt": "",
  "obj": {
  "clbcks": [
    {
      "type": "",
      "title": "",
      "fields": [
        {
          "src": "label.PNG",
          "id": "Label",
        },
        {
          "id": "MSG",
          "text": "APPROVED",
          "type": "label"
        },
        {
          "id": "amt",
          "text": {
            "text": "{0:currency}",
            "substitute": {
              "data": [
                "$requestedAmount"
              ]
            }
          },
          "type": "lbl"
        },
     ],
    }
  ]
}

And then, your look is wrong, you're looping through clbcks while you want to loop through clbcks.fields:

var order = obj.clbcks.fields;

I don't see why you would short callbacks to clbcks, just makes a sense of mess and confusion for everyone.

Luicy
  • 23
  • 1
  • 14
0

Try this :

var jsonObj = {
 "src": "Dvc",
 "rte": {
  "src": "dvc"
 },
 "msgTyp": "dvc.act",
 "srcTyp": "dvc.act",
 "cntxt": "",
 "obj": {
  "clbcks": [{
   "type": "",
   "title": "",
   "fields": [{
     "src": "label.PNG",
     "id": "Label"
    },
    {
     "id": "MSG",
     "text": "APPROVED",
     "type": "label"
    },
    {
     "id": "amt",
     "text": {
      "text": "{0:currency}",
      "substitute": {
       "data": [
        "$requestedAmount"
       ]
      }
     },
     "type": "lbl"
    }
   ]
  }]
 }
};

var amountObj = jsonObj.obj.clbcks[0].fields.find((obj) => obj.id === 'amt');

console.log(amountObj.text.substitute.data[0]);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123