

Many websites offer APIs, which will return data in JSON format. JSON is a data format that is gaining popularity and used extensively in many AJAX-powered Web sites benefits of being it's human-readable.
#Json minify python code#
If I used the code below (bottom of the post), and the directory structure inside of theme_files was such: \ I am trying to make a python script that will package the contents of a directory and minify all of the JavaScript and CSS scripts.Welcome to the online JSON Viewer, JSON Formatter, and JSON Beautifier at. JsMinified.write(rjsmin.jsmin(filename, keep_bang_comments=True)) ThemePak.writestr(os.path.join(dirname, filename), cssMinified.getvalue()) ThemePak.write(os.path.join(dirname, filename))ĬssMinified.write(rcssmin.cssmin(filename, keep_bang_comments=True)) If not filename.endswith((".css", ".js")): ThemePak = zipfile.ZipFile(pakName +".tpk", "w")įor dirname, subdirs, files in os.walk("theme_files"): PakName = input("Theme Name: ").replace(" ", "_").lower() However, the minified css and javascript files have no content inside of them other than their own file name.Įxample: the content of the file (supposedly minified) theme.css would be "theme.css" It would output the whole directory structure into the generated. ThemePak.writestr(os.path.join(dirname, filename), jsMinified.getvalue())Īs stated by and rjsmin.jsmin() expect the first element to be the CSS respectively JS code to minify as string. JsMinified.write(rjsmin.jsmin(assetdata, keep_bang_comments=True)) With open(os.path.join(dirname, filename), "r") as assetfile:Īssetdata = assetfile.read().replace("\n", "")ĬssMinified.write(rcssmin.cssmin(assetdata, keep_bang_comments=True)) You have to open and read the CSS and JS files by yourself. The changes in my if statements in the above code open the asset files as strings, then pass them along for minification. (In this case, memory object.A small tool to minify (compress) a GeoJSON file by: with open(os.path.join(dirname, filename), "r") as assetfile: I learned the hard way that you have to be sure to os.path.join() the filenames and the directories. Minify the length of the keys by mapping each key name to a single or double letter combination: option -k converts long property keys such as my_long_property_name to a or ab.Reducing the number of decimals used for coordinates: option -c 5 to keep 5 decimals. Warning: If you already have property keys like a or b, it may map them to the wrong name. only keep those features whose properties satisfy certain conditions. The tool works in two modes: for files that require reprojection or topojson output, it will read in all data at once. In case your GeoJSON file is too large for node.js to handle, this may fail. That's why it normally uses a streaming mode, so also large files can be processed easily (after which you can try reprojecting it again). You can also reduce the number of decimals for coordinates and properties, Minify (compress) each input GeoJSON or ESRI shapefile by replacing theĪttribute keys with a shorter representation (typically, its first letter). If you wish to reproject to WGS84, you can supply the EPSG code Whitelist and blacklist or filter certain properties. k, -keys Boolean Minify property keys, e.g. i, -includeKeyMap Boolean Add the key map to the GeoJSON file. b, -blacklist String Comma separated list of properties that should be KEEP those features when the property filter returns f, -filter String Comma separted list of property filters, which will r, -reproject String Reproject to WGS84 by supplying the input EPSG t, -topo Boolean Output format is TopoJSON instead of GeoJSON. w, -whitelist String Comma separated list of properties that should be kept Note that keys will notīe minified unless the -k flag is used too. c, -coordinates Positive number Only keep the first n digits of each coordinate. d, -decimals Positive number Only keep the first n digits of each decimal property. s, -src File names Source files to process: default option, you do not need toĠ1. Shrink property keys and output to $ minify-geojson -k original.geojsonĠ2. A verbose version $ minify-geojson -kv original.geojsonĠ3. Prune the blacklisted properties $ minify-geojson -b "property1, property2 "Ġ4. Keep the whitelisted properties $ minify-geojson -w "property1, property2 "Ġ5. Removes superfluous decimals (keep $ minify-geojson -c 5 original.geojsonĠ6.
