0

I expected to get JSON data from a webhook.

I get this form of data below and the content/type was application/x-www-form-urlencoded instead of application/json

results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19&results%5B0%5D%5Bname%5D=data+autre&results%5B1%5D%5Bname%5D=data2+autre&assessments%5B0%5D%5Bstatus%5D=finish&results%5B10%5D%5Bscore%5D=6&results%5B7%5D%5Bname%5D=data3&results%5B6%5D%5Bname%5D=Accept&results%5B8%5D%5Bname%5D=data4&results%5B2%5D%5Bname%5D=autres&results%5B3%5D%5Bname%5D=data6&results%5B4%5D%5Bname%5D=autre&results%5B5%5D%5Bname%5D=autres3&results%5B9%5D%5Bname%5D=data8&results%5B17%5D%5Bid%5D=18&reports%5B4%5D%5Bid%5D=8&reports%5B4%5D%5Bis_available%5D=0&results%5B7%5D%5Bscore%5D=7&results%5B17%5D%5Bscore%5D=4&reports%5B1%5D%5Bis_available%5D=1&assessments%5B2%5D%5Blink%5D=https%3A%2F%2Ftest%3D123&lastname=aaa&results%5B3%5D%5Bscore%5D=10&reports%5B3%5D%5Bid%5D=15&results%5B16%5D%5Bid%5D=17&register_link=&results%5B7%5D%5Bid%5D=8&results%5B19%5D%5Bid%5D=20&results%5B13%5D%5Bscore%5D=5&assessments%5B1%5D%5Bstatus%5D=todo&results%5B4%5D%5Bid%5D=5&status=accepted&results%5B9%5D%5Bid%5D=10&results%5B15%5D%5Bid%5D=16&results%5B3%5D%5Bid%5D=4&reports%5B4%5D%5Bname%5D=data9&reports%5B3%5D%5Bname%5D=data10&results%5B18%5D%5Bscore%5D=1&email=test@test.com&results%5B9%5D%5Bscore%5D=6&synthesis=

How can I convert this to json ?

Thanks

slama
  • 85
  • 2
  • 11
  • That text decoded is not json (it actually starts with “results[6][id]=7&results[18][id]=19&results[0][name]=data+autre”) - so it’s not just getting it decoded, but you should be finding from the webhook owner how you can request json (eg this may help : https://stackoverflow.com/questions/43209924/rest-api-use-the-accept-application-json-http-header ) – racraman Nov 01 '19 at 09:21
  • The naive json translation `{"results[6][id]" : "7", "results[18][id]" : "19", "results[0][name]" : "data autre"}` does not seem a usable data structure too. – Joop Eggen Nov 01 '19 at 09:47
  • yes @racraman i know it's not json that's why i posted this, the API owner can't change the content type from application/x-www-form-urlencoded to application/json, and if i add Accept=application/json i won't have the data anymore – slama Nov 01 '19 at 09:49
  • yes @JoopEggen it's not.. – slama Nov 01 '19 at 09:50

1 Answers1

1

if you are looking to convert this in java, may be you can try the following code:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class URLEncodeDecode {
    public static void main(String[] args) {
        String url2 = "results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19";
        String decodeURL = decode(url2);
        System.out.println("Decoded URL: " + decodeURL);

        System.out.println(Stream.of(decodeURL.split("&")).map(elem -> new String(elem)).collect(Collectors.toList()));

        List<String> uriToList = Stream.of(decodeURL.split("&")).map(elem -> new String(elem))
                .collect(Collectors.toList());

        Map<String, String> uriToListToMap = new HashMap<>();

        for (String individualElement : uriToList) {
            uriToListToMap.put(individualElement.split("=")[0], individualElement.split("=")[1]);
        }

        // Use this builder to construct a Gson instance when you need to set
        // configuration options other than the default.
        GsonBuilder gsonMapBuilder = new GsonBuilder();

        Gson gsonObject = gsonMapBuilder.create();

        String uriToJSON = gsonObject.toJson(uriToListToMap);
        System.out.println(uriToJSON);

    }

    public static String decode(String url) {
        try {
            String prevURL = "";
            String decodeURL = url;
            while (!prevURL.equals(decodeURL)) {
                prevURL = decodeURL;
                decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
            }
            return decodeURL;
        } catch (UnsupportedEncodingException e) {
            return "Issue while decoding" + e.getMessage();
        }
    }

}
shailesh p
  • 73
  • 7
  • the json i got is still contains [number][id]..... it still not usable.. btw i got the same result as you did but using only few lines. – slama Nov 01 '19 at 10:26
  • Ok, so if the string is "results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19" , what is the expected JSON output?could you please share the expected output? – shailesh p Nov 01 '19 at 12:18
  • 1
    the expected output is : {"results": [ {"id": ,"name": ,"score":}, ...], "reports" : [ {"id": ,"name": ,"is_available": ,"link": }, ... ] , "email": } something like this – slama Nov 01 '19 at 13:47