How to format the JSON string to match the "try-it-yourself" query I've made? API v2

Hello!

I have written the following mutation on the “try-it-yourself” page, and it works flawlessly.

mutation {
  change_multiple_column_values(board_id: XXXXXXXXX, item_id: XXXXXXXXX, column_values: "{\"text\":\"test1\", \"numbers7\":\"0\", \"date\" : { \"date\" : \"1955-10-28\" }}") {
    id
  }
}

However, the next step in the process for me is to integrate this into Java code. I have managed to post the data with authentication and so on which works fine. However I struggle greatly with formatting the body correctly so that the above mentioned query gets executed. Could anyone kindly help me as I am at a loss how to actually format it correctly? At the moment the error code I get by the post is code 400.

I have tried many different variations to no avail with my latest try being the following:

String body = “{ "query" : mutation { change_multiple_column_values(board_id: XXXXXXXX, item_id: XXXXXXXX, column_values: {"text":"test1", "numbers7":"0", "date" : { "date" : "1955-10-08" }}") { id }}}”;

Do note that the change in date is intentional so that I could see any change on Monday in case that my query worked.

Thank you in advance for your help.

I’m new here so I may be mistaken, but I think I was just having the same issue with formatting.
In your body it you have quotes around the data types i.e {“text”:“test1”} should be {text:“test1”} and so on for each data type. Be sure to check the data type each pair needs, I believe numbers7 would need an int not a string.

String body = “{ query: mutation { change_multiple_column_values(board_id: XXXXXXXX, item_id: XXXXXXXX, column_values: {text:“test1”, numbers7:0, date: { date : “1955-10-08” }}) { id }}}";

Hey Andreas and Alexander,

Alex is right on the mark – each column type needs a different format of data.

On top of that, you need to ensure that your program is sending the correct strings to the API. From your example, it looks like your quotes are not paired correctly. I would suggest escaping inner quotes so they don’t get interpreted literally as the end of a string:

String body = "{ \"query\" : \"mutation ... \" }"

Let me know if that helps.

Hey and thanks for the help @Alex.M and @dipro

I am testing with your advice right now and have the following body. However I haven’t managed to make it work as of yet. In case you see any mistakes please do not hesitate to say so. I will update with a new post if I manage to get it going.

    String body = "{ \"query\": \"mutation { change_multiple_column_values(board_id: XXXX, item_id: XXXX, column_values: {text:\"test1\", numbers7:100, date: { date : \"1999-01-01\" }}) { id }}\"}";

Kind regards.

I got it to work with the following string. Thanks for the help once more!

    String q = "\"mutation {change_multiple_column_values(board_id: XXXX, item_id: XXXX, column_values: \\\"{\\\\\\\"text\\\\\\\":\\\\\\\"test1\\\\\\\", \\\\\\\"numbers7\\\\\\\":\\\\\\\"10\\\\\\\", \\\\\\\"date\\\\\\\" : { \\\\\\\"date\\\\\\\" : \\\\\\\"1999-10-18\\\\\\\" }}\\\") {id}}\"";
    String v = "null";
    String body = String.format("{\"query\": %1$s, \"variables\": %2$s}", q, v);

It is far from beautiful, but it works well and can be wrapped to not be an eyesore.

1 Like