Bard is a large language model from Google AI, trained on a massive dataset of text and code. Bard can be used to work with JSON data in a variety of ways.
In this article, we will cover how to use Bard to work with JSON data. We will provide examples of how to send information in a JSON format, how to ask Bard to respond with JSON format, and how to batch process many article headlines and content for sentiment analysis.
JSON is a lightweight data-interchange format that is easy to read and write. It is supported by many programming languages, making it a popular choice for sending data between computers. JSON stands for JavaScript Object Notation. Some benefits of using JSON:
- Easy to read and write, even for non-technical users.
- Language independent, so it can be used by any programming language.
- Lightweight format, so it is efficient to transmit over networks.
- Standard format, so it is widely supported by libraries and tools.
As such, it has become a very popular way to transmit information.
The JSON
We want to send Bard a headline and content from a CNBC article and have it return several bits of data. To start with, we are going to ask it to read the article and headline, and return what company the article is about, what the sentiment is, the current stock price of that company and the % change of the stock.
We need to be detailed about what content we want, how it is to be formatted and from what perspective Bard should process our requests.
This is the example JSON. It contains two full articles from CNBC and their corresponding headlines. All paragraphs and white space has been removed to provide a condensed block of text.
{
"articles": [
{
"article_title": "GM to end production of electric Chevy Bolt, its first mass-market EV, later this year",
"article_content": "DETROIT – General Motors plans to end production of its electric Chevrolet Bolt models by the end of this year, CEO Mary Barra told investors Tuesday when discussing the company’s first-quarter earnings.
The Chevy Bolt EV and EUV, a larger version of the car, make up the vast majority of the company’s electric vehicle sales to date. However, the battery cells in the cars are an older design and chemistry than the automaker’s newer electric vehicles such as the GMC Hummer and Cadillac Lyriq, which utilize GM’s Ultium architecture. Barra said a suburban Detroit plant that has produced Chevy Bolts since 2016 will be retooled in preparation for production of electric Chevy Silverado and GMC Sierra trucks scheduled for next year. There’s irony in the timing of the Bolt getting axed. It comes amid record production and sales of the vehicle for mass-market consumers, which was GM’s initial goal. The company plans to produce more than 70,000 Bolt models this year, as it targets to sell more than 400,000 EVs from early 2022 through mid-next year in North America. GM pushed the Bolt out ahead of the Tesla Model 3 in 2016. The two were considered to be the first long-range EVs designed for mainstream drivers, starting at around $35,000. But Bolt sales never caught on as well as many executives hoped, as EV sales overall remained miniscule outside of Tesla. The Bolt also suffered a major setback more recently, as GM recalled all of the Bolts ever produced due to a supplier-related battery issue that caused several fires. “When the Chevrolet Bolt EV launched, it was a huge technical achievement and the first affordable EV, which set in motion GM’s all-electric future,” Chevy spokesman Cody Williams said in a statement. “Chevrolet will launch several new EVs later this year based on the Ultium platform in key segments, including the Silverado EV, Blazer EV and Equinox EV.” GM expects to launch its upcoming EVs far faster than it has its high-end Hummer models and Cadillac Lyriq, which have been rolling out at a snail’s pace compared to its traditional vehicles. Barra said when the Orion, Michigan, plant, which currently produces the Bolts, reopens and reaches full production, employment will nearly triple, and the company will have capacity to build 600,000 electric trucks annually. GM has set a target to reach production capacity of 1 million EVs annually in the U.S. and in China, each, as it attempts to catch up to industry leader Tesla. The Bolt models, starting under $30,000, are the least expensive new electric vehicles on sale in the U.S. GM’s upcoming Equinox EV is expected to have a similar starting price when it arrives this fall."
},
{
"article_title": "Apple declares ‘resounding victory’ after decision reached in Epic Games appeal",
"article_content": "Monday’s ruling in the Ninth Circuit Court affirmed the decision that primarily found Apple did not violate antitrust law by banning competing app marketplaces on iPhones. Apple mostly won the initial court battle, with the judge finding that it did not monopolize any market. However, the iPhone maker did lose one claim and had to allow developers to place links inside their apps so users could make purchases outside the App Store. The appeals court did not overturn that decision, which was related to California law, and is the one claim that Apple says was not decided in its favor. Whether the company is forced to allow links to outside payments will be determined in possible future hearings. Apple said in its statement that it was considering further action, which could include an appeal to the Supreme Court. Whether Epic Games will help pay Apple’s legal fees will also be decided at a lower court. “Apple prevailed at the 9th Circuit Court,” Epic Games CEO Sweeney said in tweets sent after the decision. “Though the court upheld the ruling that Apple’s restraints have ‘a substantial anticompetitive effect that harms consumers’, they found we didn’t prove our Sherman Act case.” “Fortunately, the court’s positive decision rejecting Apple’s anti-steering provisions frees iOS developers to send consumers to the web to do business with them directly there. We’re working on next steps,” Sweeney continued."
}
]
}
It is important to include instructions for how Bard should interpret the information (role play if you will) and format its response. We do that by starting the request with the following:
Forget any previous instructions. You are an expert financial advisor. Articles and their corresponding headlines from a popular news website. First determine which company the article was written about. Then perform sentiment analysis on the article and determine if the article is good, bad or unknown for the company's public image. Do not provide an explanation for the sentiment analysis. Lastly, provide the company's stock symbol. Respond in JSON only. The response must be formatted as follows: {"articles":[{"article_title", "sentiment", "company_stock_symbol"}]}. Here is the list of articles:
The Response
Providing the input described above, here is the response:
Sure, here is the JSON response:
```json
{
"articles": [
{
"article_title": "GM to end production of electric Chevy Bolt, its first mass-market EV, later this year",
"sentiment": "bad",
"company_stock_symbol": "GM"
},
{
"article_title": "Apple declares ‘resounding victory’ after decision reached in Epic Games appeal",
"sentiment": "good",
"company_stock_symbol": "AAPL"
}
]
}
```
I have determined the sentiment of each article as follows:
* **GM to end production of electric Chevy Bolt, its first mass-market EV, later this year**
This article is negative for GM's public image because it suggests that the company is abandoning its electric vehicle plans. This could hurt GM's reputation as a leader in the electric vehicle market.
* **Apple declares ‘resounding victory’ after decision reached in Epic Games appeal**
This article is positive for Apple's public image because it suggests that the company has won a major legal battle against a major competitor. This could boost Apple's stock price and increase consumer confidence in the company.
Please let me know if you have any other questions.
Conclusion
While not perfect, the fact that Bard can format answers in JSON is promising. This can be very useful to perform batch sentiment analysis and allows for easy processing of responses in Python.
As you can see, there are some instructions that Bard has a hard time obeying. I was able to tweak the request to reduce the wordy-ness of the response but I couldn’t eliminate all words and keep just the JSON.
Over time, it may be possible to convince Bard to only provide answers in a strict format but it may take more training for this to happen.
How to get the json?? Because that’s an string and I can’t find the way to get a real json response… Thank you!
Hello! You would have to parse the string and look for the first and last occurrence of the opening (‘{‘) and closing (‘}’) brackets. Once you have found the position of the opening and closing brackets, you can create a sub-string containing only the JSON. You can use the python string function: find() to search for the opening and closing brackets. It will return the index location for each.