Jeffail/gabs

Not getting value from key

Closed this issue · 2 comments

Hello,
I'm trying to parse many different unpredictable json docs, but I've come across one doc, that does not seem to be working like I think it should, any help is appreciated.
This is my code that should return "2021-09-25T15:00:00+01:00" but is returning null. I've also tried using jsonParsed.Search function but the same results.

        jsonByte, _ := json.Marshal(jsonData)
	//jsonData = string(jsonByte)

	jsonParsed, err := gabs.ParseJSON(jsonByte)
	if err != nil {
		panic(err)
	}
	fmt.Println(jsonParsed.Data())

	articleDate = trimQuote(jsonParsed.Path("datePublished").String())
	if articleDate == "" || articleDate == "null" {
		articleDate = trimQuote(jsonParsed.Path("dateCreated").String())
	}

My json doc:

{
	"@context": "https://schema.org",
	"@type": "NewsArticle",
	"mainEntityOfPage": {
		"@type": "WebPage",
		"@id": "https://www.cryptodaily.co.uk/2021/09/latest-update-on-buterins-donations"
	},
	"name": "India’s Crypto Covid Relief Announces Latest Status Of Buterin’s Donation",
	"headline": "India’s Crypto Covid Relief Announces Latest Status Of Buterin’s Donation",
	"image": [
		"https://cryptodailycdn.ams3.cdn.digitaloceanspaces.com/vitalik-billionaire.jpg"
	],
	"datePublished": "2021-09-25T15:00:00+01:00",
	"dateModified": "2021-09-25T15:00:00+01:00",
	"dateline": "The second Covid-19 wave in India proved particularly devastating for the country and saw an outpouring of help from leading personalities from the crypto space who donated considerable funds to help with relief efforts in the country.",
	"description": "The second Covid-19 wave in India proved particularly devastating for the country and saw an outpouring of help from leading personalities from the crypto space who donated considerable funds to help with relief efforts in the country. Among the many individuals who donated to India’s Covid relief efforts was Ethereum co-founder Vitalink Buterin, who donated trillions of Shiba Inu tokens towards India’s Covid relief fund. All Tokens Successfully Converted Today, India’s Crypto-Covid Relief account announced the successful conversion of the last remaining tokens from the donation of $76.2 million in Stablecoins. The account stated that it had converted 10,139,544,347,612 SHIB, amounting to 76,215,526.65 USD Coin (USDC), with Wintermute expected to transfer the entire amount to the Covid Relief Fund in the next few hours. India’s Crypto Covid Relief put out the following tweet,  “The total proceeds for converting 10,139,544,347,612 SHIB are 76,215,526.65 USDC. @wintermute_t would be transferring the said amount to our ETH wallet in the next few hours. With this, we have now converted all the $SHIB donations from @VitalikButerin into stable coins.”  Once the news was out, Shiba Inu’s Twitter account also tweeted in support, stating,  “Milestone for India, and @CryptoRelief_. Stay strong, India! The #ShibArmy is and will always be with you. A special thank you to @VitalikButerin for making the biggest crypto donation in history, utilizing $SHIB, and to promote efforts in the battle against COVID-19.”  A Herculean Task The Founder of the India Covid Relief Fund, Sandeep Nailwal, had claimed that to cash out an asset that lacked utility, along with following regulatory compliance and ensuring the funds reach the intended recipients was an arduous task to pull off. The Shiba Inu network also did not have an easy time, as the token’s value fell by 50% when Vitalik Buterin made his donation. When Shiba Inu was created, 50% of the tokens were donated to the Ethereum founder. When he donated 10% of his tokens to India, Buterin burned the remaining 90%, taking out half the supply of SHIB from the market. Rising Popularity Shiba Inu, which has often been called a Dogecoin copycat, has seen a significant rise in popularity over the past few months. The network revealed that it has more than 100,000 SHIB holders and that its Twitter handle had crossed over 900,000. All signs of a growing community. SHIB was also recently listed on Coinbase and Binance, which helped push its valuation significantly. Over $870 million were invested in SHIB within 24 hours of it being listed on Coinbase. Vitalik Buterin’s Donation Vitalik Buterin had donated Shiba Tokens worth $1.1 billion that were sent to him by the developers of the Shiba Inu token. Details of the transaction accessed through Etherscan revealed that the transaction consisted of 50.6 trillion units of the SHIB token. Before the transaction, Vitalik Buterin had removed the liquidity of the Shiba token from Uniswap’s V2 and had transferred it to Uniswap V3. India’s Crypto Covid Relief Fund had also tweeted out a statement thanking Buterin, stating,  “We thank @VitalikButerin for his donation of 50,693,552,078,053 SHIBA to @CryptoRelief_ . We plan to do a thoughtful liquidation to ensure we meet our COVID relief goals. We have decided to convert the donation slowly over a period of time.”  Disclaimer: This article is provided for informational purposes only. It is not offered or intended to be used as legal, tax, investment, financial, or other advice.",
	"author": {
		"@type": "Person",
		"name": "Amara Khatri"
	},
	"publisher": {
		"@type": "Organization",
		"name": "CryptoDaily",
		"logo": {
			"@type": "ImageObject",
			"url": "https://cryptodailycdn.ams3.cdn.digitaloceanspaces.com/logos/cd-shield.png"
		}
	}
}

Hi @infosecwatchman. Thanks for trying out gabs! I had a look at your code and I think you're trying to get a byte slice from a string, but jsonByte, _ := json.Marshal(jsonData) is not a good way to do that, because it will transform the input string by escaping all the JSON special characters. What you probably want to do is []byte(jsonData), so then you'll have:

jsonParsed, err := gabs.ParseJSON([]byte(jsonData))
if err != nil {
	panic(err)
}
fmt.Println(jsonParsed.Path("datePublished"))

which prints "2021-09-25T15:00:00+01:00".

That fixed it! Thanks!