1

I have the following JSON response coming from an API.

{
    "status": true,
    "cakes": {
        "7689": {
            "id": 7689,
            "flavor": "chocolate",
            "cookDetails": {
                "id": 101,
                "firstName": "Name1",
                "lastName": "LastName1"
            }
        },
        "7690": {
            "id": 7690,
            "flavor": "vanilla",
            "cookDetails": {
                "id": 102,
                "firstName": "Name2",
                "lastName": "LastName2"
            }
        }
    }
}

Language I'm using to parse this JSON: Javascript

Framework: ReactNative

My question is:

1. Is the JSON format correct?

2. If yes, then how do I parse it (NOTE: I don't know the value of id in cakes until I parse it)?

PS: New to the framework. Big thanks.

prasang7
  • 571
  • 2
  • 11
  • 31

2 Answers2

1

Try using this,

{
    "status": true,
    "cakes": [{
        "id": 7689,
        "flavor": "chocolate",
        "cookDetails": {
            "id": 101,
            "firstName": "Name1",
            "lastName": "LastName1"
        }
    }, {
        "id": 7690,
        "flavor": "vanilla",
        "cookDetails": {
            "id": 102,
            "firstName": "Name2",
            "lastName": "LastName2"
        }
    }]
}

for ReactNative check this :https://facebook.github.io/react-native/docs/network.html http://www.9lessons.info/2017/04/react-native-json-parsing-and-helper.html


Note below code HTML JavaScript for your understanding.


var obj = JSON.parse('{"status": true,"cakes": [{"id": 7689,"flavor": "chocolate","cookDetails": {"id": 101,"firstName": "Name1","lastName": "LastName1"}},{"id": 7690,"flavor": "vanilla","cookDetails": {"id": 102,"firstName": "Name2","lastName": "LastName2"}}]}');

<!DOCTYPE html>
<html>
<body>

<h2>Create Object from JSON String</h2>

<p id="demo"></p>

<script>

var obj = JSON.parse('{"status": true,"cakes": [{"id": 7689,"flavor": "chocolate","cookDetails": {"id": 101,"firstName": "Name1","lastName": "LastName1"}},{"id": 7690,"flavor": "vanilla","cookDetails": {"id": 102,"firstName": "Name2","lastName": "LastName2"}}]}');
document.getElementById("demo").innerHTML = obj.cakes[0].id +", "+ obj.cakes[0].flavor+", "+obj.cakes[0].cookDetails.id+", "+obj.cakes[0].cookDetails.firstName+", "+obj.cakes[0].cookDetails.lastName;

</script>

</body>
</html>
Yuyutsu
  • 2,509
  • 22
  • 38
-1

Here is valid JSON(just extra commas were removed):

{
    "status": true,
    "cakes": {
        "7689": {
            "id": 7689,
            "flavor": "chocolate",
            "cookDetails": {
                "id": 101,
                "firstName": "Name1",
                "lastName": "LastName1"
            }
        },
        "7690": {
            "id": 7690,
            "flavor": "vanilla",
            "cookDetails": {
                "id": 102,
                "firstName": "Name2",
                "lastName": "LastName2"
            }
        }
    }
}

You can parse it with plain JSON.parse call

slesh
  • 1,902
  • 1
  • 18
  • 29
  • This is not the complete answer I guess. Please be aware of complete answer before answering the question. If you have a hint just go with comments. – Vishnuvardhan Sep 27 '17 at 08:07
  • @slesh I have updated the JSON response. Can you answer the second question (How to parse it)? – prasang7 Sep 27 '17 at 08:09