1

I have an API that returns this array or object in JSON

{"orderHead":["51817","frode","","","els"],
"orderLines":[{"VareNr":"07,1437","Produkt":"Black Glue","Antall":"6","Pris":"239","Rabatt":"0%"},{"VareNr":"07,1435","Produkt":"Grey Glue","Antall":"8","Pris":"239","Rabatt":"0%"}],
"orderSum":[{"doc":"26042016_4.oln","knr":"51817","firma":"Tolga Co","oldV":1,"oldS":2868,"newV":0,"newS":0,"newSc":2}]}

As far as I can tell there's three main posts:

  • orderHead
  • orderLines
  • orderSum

Each of which contains their own sub-array. Problem is I cannot retrieve any data from it, without or without $.parseJSON();

I have tried $.grep and $.each.

Do I have to combine several "tools" to read from this object / array ? Sorry for the very very noob question :)

0

1 Answer 1

1

You can parse the JSON with the JSON.parse method

Given the following JSON

{
    "orderHead": [
        "51817",
        "frode",
        "",
        "",
        "els"
    ],
    "orderLines": [
        {
            "VareNr": "07,1437",
            "Produkt": "Black Glue",
            "Antall": "6",
            "Pris": "239",
            "Rabatt": "0%"
        },
        {
            "VareNr": "07,1435",
            "Produkt": "Grey Glue",
            "Antall": "8",
            "Pris": "239",
            "Rabatt": "0%"
        }
    ],
    "orderSum": [
        {
            "doc": "26042016_4.oln",
            "knr": "51817",
            "firma": "Tolga Co",
            "oldV": 1,
            "oldS": 2868,
            "newV": 0,
            "newS": 0,
            "newSc": 2
        }
    ]
}

You can access the values like so:

var json = '{"orderHead":["51817","frode","","","els"],\
"orderLines":[{"VareNr":"07,1437","Produkt":"Black\ Glue","Antall":"6","Pris":"239","Rabatt":"0%"},{"VareNr":"07,1435","Produkt":"Grey\ Glue","Antall":"8","Pris":"239","Rabatt":"0%"}],\
"orderSum":[{"doc":"26042016_4.oln","knr":"51817","firma":"Tolga\ Co","oldV":1,"oldS":2868,"newV":0,"newS":0,"newSc":2}]}';

var parsed = JSON.parse(json);

console.log(parsed.orderHead);

for (line of parsed.orderLines) {
  console.log(line.VareNr, line.Produkt, line.Pris);
}

Returns:

["51817", "frode", "", "", "els"]
07,1437 Black Glue 239
07,1435 Grey Glue 239

You can also access/iterate over the result like so:

// read order sum values
console.log(parsed.orderSum[0].doc, parsed.orderSum[0].knr, parsed.orderSum[0].firma);

// iterate over the order lines
for(line in parsed.orderLines) {
    console.log(parsed.orderLines[line].Produkt);
}

Which returns:

Black Glue
VM132:60 Grey Glue

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

3 Comments

Thank you it is working. However, my editor gives me a syntax error when using the for (line of parsed.orderLines) {... }. Is there any other commands that will give the same results ?
Yes, there are numerous ways to loop through the data. I'll add another example.
Thank you once again, for your great help. I finally got it to work using $.each also :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.