raineorshine
4/3/2016 - 8:18 PM

mtg json parsing

mtg json parsing

// {3}{U}{G}

function cmc(cost) {
  var [full, colorless, colored] = cost.replace(/[{}]/g, '').match(/(\d+)?(.*)/)
  return (+colorless || 0) + colored.length
}

function color(cost) {
  return cost.match(/W/i) ? 'White' : cost.match(/U/i) ? 'Blue' : cost.match(/B/i) ? 'Black' : cost.match(/R/i) ? 'Red' : cost.match(/G/i) ? 'Green' : 'Colorless'
}
// converts cards json into csv
cards.map(x => x.name + '\t' + 
  x.manaCost.replace(/[{}]/g, '') + '\t' + 
  x.cmc + '\t' + 
  x.text.replace(/\n/g, '; ')
).join('\n')

// madness
cards.filter(x => (x.text || '').match(/madness/i))
.map(x => {
  cost = ((x.text.match(/Madness ((\{.}+)*)/) || [])[1] || '').replace(/[{}]/g, '')
  return x.name + '\t' + 
  x.type + '\t' + 
  color(cost) + '\t' + 
  cost + '\t' + 
  cmc(cost) + '\t' + 
  'Madness\t' +
  x.text.replace(/\n/g, '; ').replace(/ \(.*\)/, '')
}).join('\n')
curl mtgjson.com/json/SOI.json > SOI.json
cat SOI.json | jq '.cards | map(select(.type == "Instant"))'|c