It’s my first Java project, I’m working on a piece of code to import Magic The Gathering cards info from Scryfall (JSON) and copy it into java objects for further processing. I found a problem I cannot get solved: the info of each Card is reachable on a specific URL (JSON). That JSON has a list of characteristics, but for some cards, a particular characteristic is not existing. The JSON does not contain that characteristic at all (i.e. it does not say empty/unknow, it’s simply missing). Then my code drops an error.
Is there a way to skip the parsing of a characteristic in case it’s not existing in the JSON?
I have read about using @JsonIgnoreProperties(ignoreUnknown = true), but I don’t know how to implement it in my code (if it would be valid solution since it seems to be for declaring a class, which I have not done).
This is my code so far. It works well unless a characteristic of the JSON is missing. Normally it’s the .image_uris (image of card) which is missing for some cards.
NOTE: apiURL is a variable that contains the URL (JSON) with the info of all the cards of a particular Set.
const axios = require('axios')
async function getCardsMTG(){
//I have stored the URL with all cards in "apiURL"
await axios.get(apiURL).then((card)=>{
//card.data (axios.data) gives me the raw output of the http
//(w/the 2nd) .data I access the matrix "data" of the http
for (let j = 0; j < card.data.data.length; j++) {
const itCardObj = card.data.data[j]
//For each iteration through "card.data.data" I visit a different card
//For each card, I will store all info in an object called "CardObj"
const CardObj={
"apiID":itCardObj.id||"", //UUID
"language":itCardObj.lang||"", //string
"cmc": itCardObj.cmc ||null, //int
"coloridentity": itCardObj.color_identity||"", //array of strings
"defense": itCardObj.defense ||"", //string
"imageURL": itCardObj.image_uris.normal , //string
"manacost":itCardObj.mana_cost ||"", //string
"name": itCardObj.name ||"", //string
"power": itCardObj.power ||"", //string
"toughness": itCardObj.toughness ||"", //string
"finishes": itCardObj.finishes||"", //array of strings
"promo": itCardObj.promo, //bool
"promotypes":itCardObj.promo_types ||"", //array of strings
"rarity": itCardObj.rarity ||"", //string
"setcode":itCardObj.set ||"", //string
"text": itCardObj.printed_text||"", //string
"type":itCardObj.type_line ||"", //string
}
//I inform I have captured the card
console.log(`Fetched CARD ${CardObj.name}.`)
//I print a table view of all card-info for checking purposes (will be deleted later on)
console.table(CardObj)
//I add the CARD-OBJECT to the end (push) of the array containing all CARDS
cardsArray.push(CardObj)
}
})
}