0

I am still new to JSON so I have a probably very stupid question. How do I reference
list.data[0].bom-item.descriptor? The alert statement I created didn't work for me. Thanks for help.

var txt='{"list": {"data": [{"bom-item": {"dmsID": 3949,"bomPath": "3951B3949","workspaceType": 6,"workspaceID": 8,"bomDepthLevel": 1,"quantity": 1,"formattedQuantity": "1.0","descriptor": "500-0059-000 - FCI, P681-C01 SKU77 (900-60681-0077-400) [REV:D]","revision": "D","units": "EA","itemNumber": 1,"assembly": false,"cost": 0,"isPinned": false,"quoteID": 0,"isUsingDefaultQuote": false,"leaf": false,"redlinedCost": -1,"redlineAddition": false,"redlineAgainstVersion": -1,"redlineDeletion": false,"totalWeight": 1,"lifecycleStatus": "Production","hasSourcing": false,"fields": null}}]}}'



alert(txt.list.data[0].bom-item.descriptor);
1
  • what happens when you run that alert statement? Commented Aug 13, 2013 at 14:43

2 Answers 2

1

In your current code, txt is just a string, so it has no JSON object properties. You need to use JSON.parse() to translate it to a JSON object (and use index notation as stated in another answer — the hyphen - is what's causing the trouble):

<  yourObj = JSON.parse('{"list": {"data": [{"bom-item": {"dmsID": 3949,"bomPath": "3951B3949","workspaceType": 6,"workspaceID": 8,"bomDepthLevel": 1,"quantity": 1,"formattedQuantity": "1.0","descriptor": "500-0059-000 - FCI, P681-C01 SKU77 (900-60681-0077-400) [REV:D]","revision": "D","units": "EA","itemNumber": 1,"assembly": false,"cost": 0,"isPinned": false,"quoteID": 0,"isUsingDefaultQuote": false,"leaf": false,"redlinedCost": -1,"redlineAddition": false,"redlineAgainstVersion": -1,"redlineDeletion": false,"totalWeight": 1,"lifecycleStatus": "Production","hasSourcing": false,"fields": null}}]}}')
>  [object Object]
<  yourObj.list.data[0]["bom-item"].descriptor
>  "500-0059-000 - FCI, P681-C01 SKU77 (900-60681-0077-400) [REV:D]"

Info on the JSON object at MDN.

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

Comments

1

bom-item is not a legal Javascript identifier.

Instead, you need to use indexer notation:

txt.list.data[0]["bom-item"].descriptor

Or, better yet, use camelCase instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.