-1

I'm trying to figure out how to access the value inside two dictionaries via JavaScript.

The JSON output from the server is;

{"meta":{},"linked":{custom_fields":[{"id":"4","name":"Department"}],"custom_field_values":[{"id":"0001","value":"Marketing","links":{"custom_field":{"id":"4","type":"custom_fields"}}}]

I need to list Marketing as the department. I can't seem to access "links" to pull the id.

If I create var linked = linked.custom_field_values; I get a response.

{"id":"0001","value":"Marketing","links":{"custom_field":{"id":"4","type":"custom_fields"}}}

As soon as I try to var cfl = linked.links.custom_field.id it's saying links isn't defined. So I'm not sure what I'm doing wrong in trying to create a variable for this?

Links is a dict with Custom_field right under as a dict with the values I need.

Wouldn't this print out the department correctly if everything works right?

if(cfl.id == 4){
console.log('Department is ' + linked.value);
}
  • 1
    The first block you've posted is invalid JSON, so we can't really help you with it. Please update the question with the actual server output (you can double-check it at http://jsonlint.com). – T.J. Crowder Sep 16 '17 at 08:20
  • Is `custom_field_values` an array? If so you would have to write `var linked = linked.custom_field_values[0];` to get that object. – Jim Hall Sep 16 '17 at 08:20
  • `link` does not have a property `links`. – trincot Sep 16 '17 at 08:23
  • @T.J.Crowder this is the actual JSON from the server. – computerguyinhere Sep 16 '17 at 15:52
  • @computerguyinhere: Well, that's the problem: The server code is broken. As I said, the JSON is (quite) invalid: https://jsonlint.com/?json=%7B%22meta%22%3A%7B%7D%2C%22linked%22%3A%7Bcustom_fields%22%3A%5B%7B%22id%22%3A%224%22%2C%22name%22%3A%22Department%22%7D%5D%2C%22custom_field_values%22%3A%5B%7B%22id%22%3A%220001%22%2C%22value%22%3A%22Marketing%22%2C%22links%22%3A%7B%22custom_field%22%3A%7B%22id%22%3A%224%22%2C%22type%22%3A%22custom_fields%22%7D%7D%7D%5D – T.J. Crowder Sep 17 '17 at 08:20

2 Answers2

0

It looks like custom_field_values an array. You have to look at the first item in the array like so var linked = linked.custom_field_values[0];

Jim Hall
  • 6,079
  • 2
  • 16
  • 15
  • Thanks for the responses. Can we take it one level deeper? I need to access "links" custom_field: id. Yes, custom_field_values is an array. – computerguyinhere Sep 16 '17 at 08:45
0

With a proper formatted object, you see, that you have an array inside. So you need to take an index for the array.

    var object = {
        meta: {},
        linked: {
            custom_fields: [
                {
                    id: "4",
                    name: "Department"
                }
            ],
            custom_field_values: [
                {
                    id: "0001",
                    value: "Marketing",
                    links: {
                        custom_field: {
                            id: "4",
                            type: "custom_fields"
                        }
                    }
                }
            ]
        }
    };

console.log(object.linked.custom_fields[0].id);
console.log(object.linked.custom_field_values[0].id);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392