Skip to content

Instantly share code, notes, and snippets.

@danielsan
Created April 15, 2020 19:27
Show Gist options
  • Save danielsan/4493e4f03d5b510ca7fc4b3638b79b74 to your computer and use it in GitHub Desktop.
Save danielsan/4493e4f03d5b510ca7fc4b3638b79b74 to your computer and use it in GitHub Desktop.
short-json has the goal of reducing the size in bytes of a JSON string for network transportation
var o = {"count":-1,"countTime":16274,"queryTime":23158,"data":[{"_id":"15869731151124671.9575aahr","journey":{"fullRoute":"RSW>YHZ","priceSpecification":{"priceBeforeTaxesAndFees":null,"totalPrice":343,"taxAmount":null,"isSoldOut":false,"currencyCode":"CAD","currencySymbol":"CAD","priceOffers":["JOURNEY"]},"passengerDetails":[{"category":"adult","mappedTo":"adultCount","count":1}],"passenger":{"count":1,"seniorCount":0,"adultCount":1,"youngAdultCount":0,"childCount":0,"infantInLapCount":0,"infantInSeatCount":0},"outboundFlight":{"boundType":"OUTBOUND","route":"RSW>YHZ","priceSpecification":{"totalPrice":343,"taxAmount":null},"fareClassInput":"ECONOMY","fareClass":"ECONOMY","departureAirportIataCode":"RSW","arrivalAirportIataCode":"YHZ","departureDate":"2020-06-14","departureCity":{"name":"FMY","cityCode":null},"departureCountry":{"name":"United States","isoCode":null},"arrivalCity":{"name":"YHZ","cityCode":null},"arrivalCountry":{"name":"Canada","isoCode":null},"flightType":"INTERNATIONAL"},"departureDate":"2020-06-14","returnDate":null,"journeyType":"ONE_WAY","flightType":"INTERNATIONAL","airline":{"name":"Air Canada","iataCode":"AC"},"additionalInfo":{"fields":[{"name":"original_journey_type","value":"One Way"},{"name":"market_code","value":"TNB"},{"name":"original_flight_html","value":""},{"name":"original_fare_html","value":""},{"name":"dataLayerInfo","value":{"htmlStepText":"","sEvar17Step":"depart","everymundoInfo":{"lowestPrice":{"economy":{"price":343,"flightBrand":["LCC-AC"],"boundAvailability":1},"business":{"price":1079,"flightBrand":["LCC-AC"],"boundAvailability":1}},"currencyCode":"CAD","geoOrigin":"rsw"}}}]}},"page":{"name":"Search result page","url":"https://www.aircanada.com/ca/en/aco/home.html#/faredriven:0","hostName":"www.aircanada.com","languageIsoCode":"en","siteEdition":"en-CA"},"device":{"category":"DESKTOP","screenResolution":"2560x1440","operatingSystem":"Windows"},"metadata":{"app":{"name":"Farenet","version":"ac_v3.5","datasource":"IBE_FLIGHT_SELECTION"},"trackingId":null,"schemaVersion":"1.0.0","emcid":null,"emtid":null},"priceSpecification":{"currencyCode":"CAD","currencySymbol":"CAD"},"__v":1,"__createdAt":"2020-04-15T17:51:55.112Z","__u":"41RC4N","__reqId":"47eee80bdd18"}]}
var json = JSON.stringify(o)
var shortJson = json.replace(/(?<!:)"([-\w]+)":/g, '$1:').replace(/:(?:null|true|false)\b/g, (a,b) => `:${a[1]}`)
var largeJson = shortJson.replace(/([-\w]+):(["\[\{])/g,'"$1":$2').replace(/([-\w]+):(-?[nft\d.]+[\],\}])/g,'"$1":$2').replace(/:[ntf]\b/g, a => `:${a[1]==='n'?null:a[1]==='t'}`)
console.assert(largeJson === json, 'largeJson !== json')
console.log({jsonLen: json.length, shortJsonLen: shortJson.length, bytesDiff: json.length - shortJson.length, '% reduction':((json.length - shortJson.length) / json.length *100).toFixed(2), '% of original': (shortJson.length/json.length*100).toFixed(2)})
// {jsonLen: 2236, shortJsonLen: 1979, bytesDiff: 257, % reduction: "11.49", % of original: "88.51"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment