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.

9
  • 1
    Your JSON isn't correct , check here jsonlint.com, you have 3 useless comma. 2 at the end of LastName and at the very end Commented Sep 27, 2017 at 8:00
  • JSON is incorrect. Colon is not allowed before { Commented Sep 27, 2017 at 8:00
  • JOSN is incorrect Commented Sep 27, 2017 at 8:02
  • 1
    @Miller - erm, yes, they are Commented Sep 27, 2017 at 8:03
  • 1
    Are you able to change the format of the data from the API? It would be easier for you if cakes were an array, excluding the outer id key. That way you can access the items by their index. Commented Sep 27, 2017 at 8:17

2 Answers 2

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>

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

Comments

-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

2 Comments

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.
@slesh I have updated the JSON response. Can you answer the second question (How to parse it)?

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.