0

I need my Google Firebase Database Data converted to a JSON string.

enter image description here

The desired outcome should be as follows:

var dataSet =[
{arr:"test",des:"DMM",eta:"17 Feb 2018 11:00",etd:"17 Feb 2018 13:30",gate:"S92", inbound:"RT456", org:"ARN", outbound:"RT678", remarks:"CHARTER", sta:"17 Feb 2018 11:00", std:"17 Feb 2018 13:30", whs:"T11"},
{arr:"test",des:"ESB",eta:"17 Feb 2018 09:00",etd:"17 Feb 2018 15:30",gate:"S94", inbound:"SD941", org:"JNB", outbound:"SD942", remarks:"", sta:"17 Feb 2018 09:00", std:"17 Feb 2018 15:30", whs:"T11"}
             ];

I'm new to this and have therefore no clue how to accomplish this.

2 Answers 2

1

First get the DataSet-schedule object and then iterate for each of the objects inside it :

dbRef.child("DataSet-schedule").once("value").then(
    function(snapshot){
        var dataSetObj = snapshot.val(), text= "";
        for(x in dataSetObj){
            text += JSON.stringify(dataSetObj[x]);
            text+= "--";  //separator
        }
    var result = text.split("--");
    result.pop();
    }

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

4 Comments

Thanks for trying. I get an error on the first line Uncaught ReferenceError: dbRef is not defined I tried changing it into firebase.database().ref("DataSet-Schedule").once("value").then( but then turns into another error message Uncaught (in promise) TypeError: JSON.Stringify is not a function
@MK01111000 There's a slight typo in my code. The function's name is JSON.stringify() and not JSON.Stringify(). So, the code will work if you write JSON.stringify(dataSetObj[x])
Also after you do result = text.split("--"); make sure you pop the last element of the result array because it contains empty string. So your code would look like var result = text.split("--"); result.pop();
Thanks, that was what I was looking for
1

The JSON data is available at https://your-project.firebaseio.com/.json. You can obtain this by sending a GET request to this url. If you have classes in your data and would like to get the data for a particular class instead, use https://your-project.firebaseio.com/your-class.json

You can test this by using curl https://your-project.firebaseio.com/.json For better readability you can use curl https://your-project.firebaseio.com/.json?print=pretty

Refer: https://firebase.google.com/docs/database/rest/retrieve-data

1 Comment

@Anuuraag Baishya, thats very helpful indeed. Only thing is that I need the data without the push-keys, like in the "desired outcome" mentioned in my first post

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.