We help IT Professionals succeed at work.

JSON: Fix My Format

hankknight
hankknight asked
on
My JSON data is not formatted correctly.  It only recognizes one fruit instead of three.

I think the problem is that I did not properly close the first fruit correctly, but I don't know how to fix it.
{
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }, {
            "py": [{
                "@attributes": {
                    "Name": "Papaya",
                    "Calories": "163",
                    "Protein": "0.61 g",
                }
            }],
            "mn": [{
                "@attributes": {
                    "Name": "Mango",
                    "Calories": "101",
                    "Protein": "0.8 g",
                }
            }],
        }]
    }
}

Open in new window

Comment
Watch Question

Commented:
You've missed one bracket.

{
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }], {
            "py": [{
                "@attributes": {
                    "Name": "Papaya",
                    "Calories": "163",
                    "Protein": "0.61 g",
                }
            }],
            "mn": [{
                "@attributes": {
                    "Name": "Mango",
                    "Calories": "101",
                    "Protein": "0.8 g",
                }
            }],
        }]
    }
}

Open in new window

Commented:
Sorry, I forgot to indicate where is missing in previous post.

{
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }]  // Here
, {
            "py": [{
                "@attributes": {
                    "Name": "Papaya",
                    "Calories": "163",
                    "Protein": "0.61 g",
                }
            }],
            "mn": [{
                "@attributes": {
                    "Name": "Mango",
                    "Calories": "101",
                    "Protein": "0.8 g",
                }
            }],
        }]
    }
}

Open in new window

Author

Commented:
That does NOT help.  The second two fruits are still showing up as children of the first fruit.

Test it here and you will see what levels the fruit are on:
http://jsbeautifier.org/

Commented:
Try this

{
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }],
        "py": [{
            "@attributes": {
                "Name": "Papaya",
                "Calories": "163",
                "Protein": "0.61 g",
            }
        }],
        "mn": [{
            "@attributes": {
                "Name": "Mango",
                "Calories": "101",
                "Protein": "0.8 g",
            }
        }],
    }]
}

Open in new window

Author

Commented:
That still does not work.

Please test it with my code, thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">

data = {
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }, {
            "py": [{
                "@attributes": {
                    "Name": "Papaya",
                    "Calories": "163",
                    "Protein": "0.61 g",
                }
            }],
            "mn": [{
                "@attributes": {
                    "Name": "Mango",
                    "Calories": "101",
                    "Protein": "0.8 g",
                }
            }],
        }]
    }
}
var items = [];
$.each(data, function(key, val) {
    $.each(val, function(key, val) {
        alert(key);
    });
});

</script>
<title>Test</title>
</head>
<body>
</body>
</html>

Open in new window

Author

Commented:
In the example below, there should be an alert for "gv" and "py" and "mn" however there is only one alert:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

data = {
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }, {
            "py": [{
                "@attributes": {
                    "Name": "Papaya",
                    "Calories": "163",
                    "Protein": "0.61 g",
                }
            }],
            "mn": [{
                "@attributes": {
                    "Name": "Mango",
                    "Calories": "101",
                    "Protein": "0.8 g",
                }
            }],
        }]
    }
}
var items = [];
$.each(data, function(key, val) {
    $.each(val, function(key, val) {
        alert(key);
    });
});

</script>
<title>Test</title>
</head>
<body>
</body>
</html>

Open in new window

Commented:
Try this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

data = {
    "Fruit": {
        "gv": [{
            "@attributes": {
                "Name": "Guava",
                "Calories": "112",
                "Protein": "4.2 g",
            }
        }],
		"py": [{
			"@attributes": {
				"Name": "Papaya",
				"Calories": "163",
				"Protein": "0.61 g",
			}
		}],
		"mn": [{
			"@attributes": {
				"Name": "Mango",
				"Calories": "101",
				"Protein": "0.8 g",
			}
		}]
    }
}
var items = [];
$.each(data, function(key, val) {
    $.each(val, function(key, val) {
        alert(key);
    });
});

</script>
<title>Test</title>
</head>
<body>
</body>
</html>

Open in new window