0

I first encountered this problem after scrapping data from a news website to get the info of an article. The data I got from the website was automatically stored in JSON format with the title of most of the dictionaries starting with the @ character ex("@Context": "value"). Some difficulty that prevented me from solving this was how javascript has you get info from a dictionary, ex(dictionary.key = value)and I can't start with any special characters

Here is my code

    function getArticle(url){
        URL = 'http://127.0.0.1:5000/defensedaily'
        $.ajax({
            method:"GET",
            beforeSend: function(request) {
                request.setRequestHeader("url", url);
                },
            url: URL,
            success:function(response){
                Response = response
                console.log(Response)
                return(Response)
            }
        })
    }

    var json = getArticle("http://defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/")
    console.log(json.url)

And this is the JSON being given from the API

    {
        "@context": "https://schema.org",
        "@graph": [
            {
                "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#article",
                "@type": "Article",
                "articleSection": [
                    "Budget",
                    "Congress"
                ],
                "author": {
                    "@id": "https://www.defensedaily.com/#/schema/person/f55973bb734bd7a822261b77441040ba",
                    "name": "Matthew Beinart"
                },
                "commentCount": 0,
                "dateModified": "2022-12-23T22:09:40+00:00",
                "datePublished": "2022-12-23T21:35:51+00:00",
                "headline": "Congress Sends FY ‘23 Omnibus With $858 Billion For Defense To President’s Desk",
                "image": {
                    "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#primaryimage"
                },
                "inLanguage": "en-US",
                "isPartOf": {
                    "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/"
                },
                "mainEntityOfPage": {
                    "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/"
                },
                "potentialAction": [
                    {
                        "@type": "CommentAction",
                        "name": "Comment",
                        "target": [
                            "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#respond"
                        ]
                    }
                ],
                "publisher": {
                    "@id": "https://www.defensedaily.com/#organization"
                },
                "thumbnailUrl": "https://www.defensedaily.com/wp-content/uploads/2021/11/leahy.jpg",
                "wordCount": 503
            },
            {
                "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/",
                "@type": "WebPage",
                "breadcrumb": {
                    "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#breadcrumb"
                },
                "dateModified": "2022-12-23T22:09:40+00:00",
                "datePublished": "2022-12-23T21:35:51+00:00",
                "description": "Congress has passed the final 12-bill, $1.7 trillion fiscal year 2023 omnibus appropriations package, which includes $858 billion in national defense",
                "image": {
                    "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#primaryimage"
                },
                "inLanguage": "en-US",
                "isPartOf": {
                    "@id": "https://www.defensedaily.com/#website"
                },
                "name": "Congress Sends FY ‘23 Omnibus With $858 Billion For Defense To President’s Desk - Defense Daily",
                "potentialAction": [
                    {
                        "@type": "ReadAction",
                        "target": [
                            "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/"
                        ]
                    }
                ],
                "primaryImageOfPage": {
                    "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#primaryimage"
                },
                "thumbnailUrl": "https://www.defensedaily.com/wp-content/uploads/2021/11/leahy.jpg",
                "url": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/"
            },
            {
                "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#primaryimage",
                "@type": "ImageObject",
                "caption": "Sen. Patrick Leahy (D-Vt.), chair of the Senate Appropriations Committee is retiring in 2022.",
                "contentUrl": "https://www.defensedaily.com/wp-content/uploads/2021/11/leahy.jpg",
                "height": 1670,
                "inLanguage": "en-US",
                "url": "https://www.defensedaily.com/wp-content/uploads/2021/11/leahy.jpg",
                "width": 2511
            },
            {
                "@id": "https://www.defensedaily.com/congress-sends-fy-23-omnibus-with-858-billion-for-defense-to-presidents-desk/congress/#breadcrumb",
                "@type": "BreadcrumbList",
                "itemListElement": [
                    {
                        "@type": "ListItem",
                        "item": "https://www.defensedaily.com/",
                        "name": "Home",
                        "position": 1
                    },
                    {
                        "@type": "ListItem",
                        "name": "Congress Sends FY ‘23 Omnibus With $858 Billion For Defense To President’s Desk",
                        "position": 2
                    }
                ]
            },
            {
                "@id": "https://www.defensedaily.com/#website",
                "@type": "WebSite",
                "description": "",
                "inLanguage": "en-US",
                "name": "Defense Daily",
                "potentialAction": [
                    {
                        "@type": "SearchAction",
                        "query-input": "required name=search_term_string",
                        "target": {
                            "@type": "EntryPoint",
                            "urlTemplate": "https://www.defensedaily.com/?s={search_term_string}"
                        }
                    }
                ],
                "publisher": {
                    "@id": "https://www.defensedaily.com/#organization"
                },
                "url": "https://www.defensedaily.com/"
            },
            {
                "@id": "https://www.defensedaily.com/#organization",
                "@type": "Organization",
                "image": {
                    "@id": "https://www.defensedaily.com/#/schema/logo/image/"
                },
                "logo": {
                    "@id": "https://www.defensedaily.com/#/schema/logo/image/",
                    "@type": "ImageObject",
                    "caption": "Defense Daily",
                    "contentUrl": "https://www.defensedaily.com/wp-content/uploads/2019/01/dd.png",
                    "height": 58,
                    "inLanguage": "en-US",
                    "url": "https://www.defensedaily.com/wp-content/uploads/2019/01/dd.png",
                    "width": 530
                },
                "name": "Defense Daily",
                "sameAs": [],
                "url": "https://www.defensedaily.com/"
            },
            {
                "@id": "https://www.defensedaily.com/#/schema/person/f55973bb734bd7a822261b77441040ba",
                "@type": "Person",
                "description": "Reporter: Cyber Security/IT/Military Defense Daily Ph: 240-477-2677",
                "image": {
                    "@id": "https://www.defensedaily.com/#/schema/person/image/",
                    "@type": "ImageObject",
                    "caption": "Matthew Beinart",
                    "contentUrl": "https://www.defensedaily.com/wp-content/uploads/2018/04/matthew-beinart_300x300px_transparent.png",
                    "inLanguage": "en-US",
                    "url": "https://www.defensedaily.com/wp-content/uploads/2018/04/matthew-beinart_300x300px_transparent.png"
                },
                "name": "Matthew Beinart",
                "sameAs": [
                    "https://twitter.com/mbeinart22"
                ],
                "url": "https://www.defensedaily.com/author/mbeinart/"
            }
        ]
    }

I'm expecting to be able to get the value of a key in javascript with it having a special character.

Caminero
  • 265
  • 3
  • 19

1 Answers1

0

For this you can use [] syntax, inside the brackets, you can put a string as a key value (instead of an integer for an array). Make sure you also parse the JSON data with JSON.parse().

Example: data["@context"]

EpicPuppy613
  • 301
  • 1
  • 13