def generate_json_artifacts(app, pagename, templatename, context, doctree):
try:<EOL><INDENT>build_json = os.path.abspath(<EOL>os.path.join(app.outdir, '<STR_LIT:..>', '<STR_LIT>')<EOL>)<EOL>outjson = os.path.join(build_json, pagename + '<STR_LIT>')<EOL>outdir = os.path.dirname(outjson)<EOL>if not os.path.exists(outdir):<EOL><INDENT>os.makedirs(outdir)<EOL><DEDENT>with open(outjson, '<STR_LIT>') as json_file:<EOL><INDENT>to_context = {<EOL>key: context.get(key, '<STR_LIT>')<EOL>for key in KEYS<EOL>}<EOL>json.dump(to_context, json_file, indent=<NUM_LIT:4>)<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>log.exception(<EOL>'<STR_LIT>'.format(page=outjson)<EOL>)<EOL><DEDENT>except IOError:<EOL><INDENT>log.exception(<EOL>'<STR_LIT>'.format(page=outjson)<EOL>)<EOL><DEDENT>except Exception as e:<EOL><INDENT>log.exception(<EOL>'<STR_LIT>'.format(page=outjson)<EOL>)<EOL><DEDENT>
Generate JSON artifacts for each page.

This way we can skip generating this in other build step.