Forked from aayla-secura/JSON_to_URL_encoded_form.py
Created
September 29, 2022 00:38
-
-
Save bglopez/edd0197b5761a72b9ffd5d067c62238c to your computer and use it in GitHub Desktop.
Revisions
-
aayla-secura revised this gist
Oct 11, 2020 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,10 @@ parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=''' JSON to URL-encoded form converter. If neither --object nor --file is given, the JSON object is read from standard input.''') input_parser = parser.add_mutually_exclusive_group() input_parser.add_argument( '--object', '-o', dest='obj', @@ -20,7 +23,7 @@ parser.add_argument( '--no-plus', dest='quoter', action='store_const', const=quote, default=quote_plus, help='Encode a space with %%20 instead of +.') parser.add_argument( '--safe', '-s', dest='safe', help='Safe characters to skip encoding.') -
aayla-secura revised this gist
Oct 11, 2020 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,9 +34,10 @@ data_s = args.obj else: # read JSON data from stdin data_s = '' while True: line = sys.stdin.readline() if not line.strip(' \r\n'): break data_s += line -
aayla-secura created this gist
Oct 11, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ #!/usr/bin/env python3 import json from urllib.parse import quote, quote_plus import sys import os import argparse parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='JSON to URL-encoded form converter') input_parser = parser.add_mutually_exclusive_group() input_parser.add_argument( '--object', '-o', dest='obj', help='JSON object to parse.') input_parser.add_argument( '--file', '-f', dest='file', help='JSON file to parse.') parser.add_argument( '--no-plus', dest='quoter', action='store_const', const=quote, default=quote_plus, help='Encode a space with %20 instead of +.') parser.add_argument( '--safe', '-s', dest='safe', help='Safe characters to skip encoding.') args = parser.parse_args() if args.file is not None: with open(args.file, 'r') as f: data_s = f.read() elif args.obj is not None: data_s = args.obj else: # read JSON data from stdin while True: line = sys.stdin.readline() if not line.strip('\r\n'): break data_s += line if not data_s: sys.exit(0) data_dec = json.loads(data_s) def JSON_to_URL_encode(value, key=None): def quot(v, **kargs): if v is None: return '' if args.safe is not None: safe = kargs.pop('safe', '') + args.safe kargs['safe'] = safe return args.quoter('{!s}'.format(v), **kargs) def enc(k, v): return '{}={}'.format(quot(k, safe='[]'), quot(v)) if isinstance(value, dict): iterator = value.items() elif isinstance(value, list): iterator = enumerate(value) elif key is None: # top level object was neither list nor dictionary raise TypeError('Only lists and dictionaries supported') else: return enc(key, value) res_l = [] for k, v in iterator: if key is None: this_key = k else: this_key = '{}[{}]'.format(key, k) res_l.append(JSON_to_URL_encode(v, key=this_key)) return '&'.join(res_l) print(JSON_to_URL_encode(data_dec))