Skip to content

Issues when adding values to firebase database #704

Closed

Description

@jussihi

Hello, I'm having some issues adding values to a firebase database using this json library.

The test["pi"] adds into the firebase very well, but then when I try to add the test["pia"]["pia"] it responds with a "error" : "Invalid data; couldn't parse JSON object, array, or value."

Here is my code:

`json test;
 test["pi"] = 1.41;
 test["pia"]["pia"] = 2.0; 
 CURLcode res;
 struct curl_slist *headers = NULL;
 headers = curl_slist_append(headers, "Content-Type: application/json");
 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
 curl_easy_setopt(curl, CURLOPT_URL, "https://aaaa.firebaseio.com/bbbb.json");
 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, moi.dump());
 res = curl_easy_perform(curl);
 std::cout << res << std::endl;
 curl_slist_free_all(headers);
 curl_easy_cleanup(curl);`

Activity

  1. jussihi commented on Aug 22, 2017

    @jussihi
    Author

    I found this with a solution that I should wrap my JSON into single quotes:
    https://stackoverflow.com/questions/33683757/firebase-invalid-data-couldnt-parse-json-object

    Can I do it automatically with this json library?

  2. jussihi commented on Aug 22, 2017

    @jussihi
    Author

    More input; if I wirte this code:

    test["asd2"] = {{"asd3","asd2"},{"asd4","asd5"}};
    std::string s = test.dump();
    std::cout << s << std::endl;
    

    I get output {"asd2":{"asd3":"asd2","asd4":"asd5"}} from the cmd.

    Now if I use libcurl from cmd, with input:
    curl -H 'Content-Type:application/json' -X PUT -d "{\"asd2\" :{\"asd3\": \"asd2\", \"asd4\":\"asd5\"}}" "https://....

    I get the exact same output ( {"asd2":{"asd3":"asd2","asd4":"asd5"}} ), but only using libcurl from terminal succees.. What is going on?

  3. nlohmann commented on Aug 22, 2017

    @nlohmann
    Owner

    No, adding single quotes around a JSON text is not supported by the library, but can be done without problems in client code.

    Unfortunately, I do not understand your third comment #704 (comment). What is the issue?

  4. jussihi commented on Aug 22, 2017

    @jussihi
    Author

    Thank you very much for responding. I'm currently pulling my hair off because of this. Yes as you can see from this post it doesn't seem to need those single quotes anymore.. But here is what I got:

    So, giving this exact input to my curl.exe straight from terminal (windows CMD) works;
    curl -H 'Content-Type:application/json' -X PUT -d "{\"asd2\" :1.41, \"pi2\" : 2.82}" "https://

    It inputs no problem; also giving raw string from my C++ program like this works;
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"pi\":1.41,\"pi2\":2.82}");

    but then when I try to feed it with your json library output, it doesn't work:
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, j.dump());

    and on top of that both "{\"pi\":1.41,\"pi2\":2.82}" and j.dump() seem exactly the same (at least to my eye) when printed out from my C++ program.

    and more info: I'm using google firebase and its REST api documented here https://firebase.google.com/docs/reference/rest/database/

  5. nlohmann commented on Aug 22, 2017

    @nlohmann
    Owner

    That's weird. Can you make sure the strings are identical to the byte? There may be some subtle issue with escaping.

  6. jussihi commented on Aug 22, 2017

    @jussihi
    Author

    Can you point me a way to make clear they are identical?

    Can I just memcmp with j.dump().data() and string.data() ?

  7. nlohmann commented on Aug 22, 2017

    @nlohmann
    Owner

    Well, storing both in a std::string and check if they are equal with == should be sufficient. It's just a guess, but the first thing I would try if they "look" the same, but treated differently.

  8. theodelrieu commented on Aug 22, 2017

    @theodelrieu
    Contributor

    I might miss something here, but are you passing a std::string to the curl API?
    Does that even compile?

  9. jussihi commented on Aug 22, 2017

    @jussihi
    Author

    @nlohmann the strings are the same

    @theodelrieu yes it does, without warnings. and now as you ask, yes, it works if I put in j.dump().data()

    Now the firebase response is "No data supplied" :(

    nvm, it works, you can close it. Thanks @theodelrieu for opening my eyes.

  10. theodelrieu commented on Aug 22, 2017

    @theodelrieu
    Contributor

    @jussihi It's because of varargs I guess, that's weird, glad it fixes your problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions