Michael Eliot Michael Eliot - 1 month ago 13
JSON Question

JSON to Firebase

I'm new to both firebase and JSON, and I'm having a weird issue where it starts the firebase database with a zero.
JSON:

[{
"Location" : {
"0" : {
"name" : "Huntington Beach",
"address" : "602 Pacific Coast Hwy, Huntington Beach, CA 92648",
"hours" : "Sun 8am-8pm, Mon - Sat 8am-9pm",
"phoneNumber" : "(714) 536-TACO(8226)"
},
"1" : {
"name" : "Newport Beach",
"address" : "3014 W Balboa Blvd, Newport Beach, CA 92663",
"hours" : "Sun 8am-8pm, Mon - Sat 8am-9pm",
"phoneNumber" : "(949) 723-TACO(8226)"
}
}
}]


Firebase After Import

Answer

As stated in the comments, your problem is the squared brackets [].

[{ // <--------- THIS [
"Location" : {
    "0" : {
        "name" : "Huntington Beach",
        "address" : "602 Pacific Coast Hwy, Huntington Beach, CA 92648",
        "hours" : "Sun 8am-8pm, Mon - Sat 8am-9pm",
        "phoneNumber" : "(714) 536-TACO(8226)"
    },
    "1" : {
        "name" : "Newport Beach",
        "address" : "3014 W Balboa Blvd, Newport Beach, CA 92663",
        "hours" : "Sun 8am-8pm, Mon - Sat 8am-9pm",
        "phoneNumber" : "(949) 723-TACO(8226)"
    }
}
}] // <--------- THIS ]

Those brackets indicate that you're saving an Array. An array is a container of objects. And even tho you only got one object in your container, your Firebase sets a number for it, because it does not know upfront the number of objects. And 0 because in informatics 0 = 1

Delete the [] or (as I know from your other question, that you are a Swift Developer), take the object out of your Array. Like:

let yourArray: [JSON] = []
for element in yourArray {
    yourUploadFunc(element)
}