2

Trying to fetch JSON-LD data from a URL in JAVA. But the result shows up in HTML.

Just before the JSON data starts, it shows this message.

<!doctype html><!-- This is an HTML rendering of the ConceptNet API response. We're showing you HTML so you get convenient syntax highlighting and links, because we think you're seeing this in a Web browser, based on your 'Accept: text/html' header. If you're using a client that isn't a Web browser, you probably wanted pure JSON. In that case, set the header 'Accept: */*' or 'Accept: application/json' on your request, or add the '?format=json' parameter.

How do I set the appropriate header to get the string in pure JSON format instead of HTML.

The code is

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class test{
    public static void main(String[] args) throws IOException {
        URL url=new URL("http://api.conceptnet.io/c/en/french_toast");
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();

        int responsecode=conn.getResponseCode();
        System.out.println(responsecode);
        if(responsecode!=200){
            throw new RuntimeException("HttpResponseCode:"+responsecode);

        }
        else{
            Scanner sc=new Scanner(url.openStream());
            String inline="";
            while(sc.hasNext()){
                inline+=sc.nextLine();

            }
            System.out.println("\nJson data in String format");
            System.out.println(inline);
            sc.close();


        }


    }


} 

1
  • 1
    What is the code you are using to get this json? If it is too large, try creating a minimal reproducible example Commented Feb 10, 2020 at 19:12

4 Answers 4

1

Please add ?format=json at the end of your Url, no more action needed.

Like:

 URL url=new URL("http://api.conceptnet.io/c/en/french_toast?format=json");

Your Code:

public static void main(String[] args) throws IOException {

        //Only this line (Url) has been edited (?format=json added)
        URL url = new URL("http://api.conceptnet.io/c/en/french_toast?format=json");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();

        int responsecode = conn.getResponseCode();
        System.out.println(responsecode);
        if (responsecode != 200) {
            throw new RuntimeException("HttpResponseCode:" + responsecode);

        } else {
            Scanner sc = new Scanner(url.openStream());
            String inline = "";
            while (sc.hasNext()) {
                inline += sc.nextLine();

            }
            System.out.println("\nJson data in String format");
            System.out.println(inline);
            sc.close();

        }

    }

Result :

Json data in String format
{"@context": ["http://api.conceptnet.io/ld/conceptnet5.7/context.ld.json"], "@id": "/c/en/french_toast", "edges": [{"@id": "/a/[/r/Synonym/,/c/fr/pain_perdu/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/fr/pain_perdu/n/wn/food", "@type": "Node", "label": "Pain perdu", "language": "fr", "sense_label": "n, food", "term": "/c/fr/pain_perdu"}, "surfaceText": "[[Pain perdu]] is a translation of [[French toast]]", "weight": 3.4641016151377544}, {"@id": "/a/[/r/Synonym/,/c/pt/rabanada/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/pt/rabanada/n/wn/food", "@type": "Node", "label": "rabanada", "language": "pt", "sense_label": "n, food", "term": "/c/pt/rabanada"}, "surfaceText": "[[rabanada]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/ja/フレンチトースト/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/ja/フレンチトースト/n/wn/food", "@type": "Node", "label": "フレンチトースト", "language": "ja", "sense_label": "n, food", "term": "/c/ja/フレンチトースト"}, "surfaceText": "[[フレンチトースト]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/RelatedTo/,/c/en/french_toast/n/,/c/fr/pain_perdu/]", "@type": "Edge", "dataset": "/d/wiktionary/fr", "end": {"@id": "/c/fr/pain_perdu", "@type": "Node", "label": "pain perdu", "language": "fr", "term": "/c/fr/pain_perdu"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/RelatedTo", "@type": "Relation", "label": "RelatedTo"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/fr/]", "@type": "Source", "contributor": "/s/resource/wiktionary/fr", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/th/เฟรนช์โทสต์/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/th/เฟรนช์โทสต์/n/wn/food", "@type": "Node", "label": "เฟรนช์โทสต์", "language": "th", "sense_label": "n, food", "term": "/c/th/เฟรนช์โทสต์"}, "surfaceText": "[[เฟรนช์โทสต์]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/ja/フレンチ_トースト/n/,/c/en/french_toast/]", "@type": "Edge", "dataset": "/d/jmdict", "end": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/jmdict/1.07", "@type": "Source", "contributor": "/s/resource/jmdict/1.07"}], "start": {"@id": "/c/ja/フレンチ_トースト/n", "@type": "Node", "label": "フレンチ トースト", "language": "ja", "sense_label": "n", "term": "/c/ja/フレンチ_トースト"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/fr/pain_doré/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/fr/pain_doré/n/wn/food", "@type": "Node", "label": "pain doré", "language": "fr", "sense_label": "n, food", "term": "/c/fr/pain_doré"}, "surfaceText": "[[pain doré]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/IsA/,/c/en/french_toast/n/wn/food/,/c/en/dish/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/dish/n/wn/food", "@type": "Node", "label": "dish", "language": "en", "sense_label": "n, food", "term": "/c/en/dish"}, "license": "cc:by/4.0", "rel": {"@id": "/r/IsA", "@type": "Relation", "label": "IsA"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "surfaceText": "[[French toast]] is a type of [[dish]]", "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/sh/prženica/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/sh/prženica", "@type": "Node", "label": "prženica", "language": "sh", "term": "/c/sh/prženica"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/ja/フレンチトースト/n/,/c/en/french_toast/]", "@type": "Edge", "dataset": "/d/jmdict", "end": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/jmdict/1.07", "@type": "Source", "contributor": "/s/resource/jmdict/1.07"}], "start": {"@id": "/c/ja/フレンチトースト/n", "@type": "Node", "label": "フレンチトースト", "language": "ja", "sense_label": "n", "term": "/c/ja/フレンチトースト"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/zh/西多/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/zh/西多", "@type": "Node", "label": "西多", "language": "zh", "term": "/c/zh/西多"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/HasContext/,/c/en/french_toast/n/,/c/en/culinary/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/en/culinary", "@type": "Node", "label": "culinary", "language": "en", "term": "/c/en/culinary"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/HasContext", "@type": "Relation", "label": "HasContext"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/ExternalURL/,/c/en/french_toast/n/wn/food/,/http://wordnet-rdf.princeton.edu/wn31/107884176-n/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "http://wordnet-rdf.princeton.edu/wn31/107884176-n", "@type": "Node", "label": "107884176-n", "site": "wordnet-rdf.princeton.edu", "site_available": true, "term": "http://wordnet-rdf.princeton.edu/wn31/107884176-n"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/ExternalURL", "@type": "Relation", "label": "ExternalURL"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/fr/pain_perdu/n/,/c/en/french_toast/]", "@type": "Edge", "dataset": "/d/wiktionary/fr", "end": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/fr/]", "@type": "Source", "contributor": "/s/resource/wiktionary/fr", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/fr/pain_perdu/n", "@type": "Node", "label": "pain perdu", "language": "fr", "sense_label": "n", "term": "/c/fr/pain_perdu"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/nl/wentelteefje/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/nl/wentelteefje", "@type": "Node", "label": "wentelteefje", "language": "nl", "term": "/c/nl/wentelteefje"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/ExternalURL/,/c/en/french_toast/,/http://wikidata.dbpedia.org/resource/Q197973/]", "@type": "Edge", "dataset": "/d/dbpedia/en", "end": {"@id": "http://wikidata.dbpedia.org/resource/Q197973", "@type": "Node", "label": "Q197973", "site": "wikidata.dbpedia.org", "site_available": true, "term": "http://wikidata.dbpedia.org/resource/Q197973"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/ExternalURL", "@type": "Relation", "label": "ExternalURL"}, "sources": [{"@id": "/s/resource/dbpedia/2015/en", "@type": "Source", "contributor": "/s/resource/dbpedia/2015/en"}], "start": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/DerivedFrom/,/c/en/french_toast/,/c/en/french/a/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/en/french/a", "@type": "Node", "label": "french", "language": "en", "sense_label": "a", "term": "/c/en/french"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/DerivedFrom", "@type": "Relation", "label": "DerivedFrom"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/ms/roti_bakar/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/ms/roti_bakar", "@type": "Node", "label": "roti bakar", "language": "ms", "term": "/c/ms/roti_bakar"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/ExternalURL/,/c/en/french_toast/,/http://dbpedia.org/resource/French_toast/]", "@type": "Edge", "dataset": "/d/dbpedia/en", "end": {"@id": "http://dbpedia.org/resource/French_toast", "@type": "Node", "label": "French_toast", "site": "dbpedia.org", "site_available": true, "term": "http://dbpedia.org/resource/French_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/ExternalURL", "@type": "Relation", "label": "ExternalURL"}, "sources": [{"@id": "/s/resource/dbpedia/2015/en", "@type": "Source", "contributor": "/s/resource/dbpedia/2015/en"}], "start": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/zh/法兰西吐司/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/zh/法兰西吐司", "@type": "Node", "label": "法兰西吐司", "language": "zh", "term": "/c/zh/法兰西吐司"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}], "view": {"@id": "/c/en/french_toast?offset=0&limit=20", "@type": "PartialCollectionView", "comment": "There are more results. Follow the 'nextPage' link for more.", "firstPage": "/c/en/french_toast?offset=0&limit=20", "nextPage": "/c/en/french_toast?offset=20&limit=20", "paginatedProperty": "edges"}}
Sign up to request clarification or add additional context in comments.

Comments

1

Okay, since you are using HttpURLConnection, there is a method you can use to set the header just like this.

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Accept", "application/json");

8 Comments

Yeah, that's what the most resources say. But where exactly do i need to change the headers in the code?
Check out Postman, it is a good tool for making api requests. There is a section on the app that will let you specify headers. Updating my post to reflect this
@RaghavRathi Also: postman will really only be useful for testing, it won't be part of your code. Is there documentation for this api? Maybe they tell you how to specify the response type you want?
@RaghavRathi check out my post, just edited it. Looks like there is a method in the URLConnection class that will let you do this
@RaghavRathi hmm, try changing conn.setRequestProperty("Accept", "application/json"); to conn.setRequestProperty("Content-Type", "application/json");
|
1

Use an HTML parser e.g. jsoup.

import java.io.IOException;
import java.net.URL;

import org.jsoup.Jsoup;

public class Main {
    public static void main(String args[]) throws IOException {
        System.out.println(Jsoup.parse(new URL("http://api.conceptnet.io/c/en/french_toast"), 10000).body().text());
    }
}

Output:

{
  "@context": [
    "http://api.conceptnet.io/ld/conceptnet5.7/context.ld.json"
  ],
  "@id": "/c/en/french_toast",
  "edges": [
    {
...
...
...

Comments

0

Try the same request with a tool like postman or a browser rest api-plugin.

You can modify your request header with these tools, which you can't do with a browser. Setting the request header content type to application/json solves this behaviour.

Comments

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.