0

I m writing command line script in python to execute my scala code. but I m struct in formating the input json with List.

see below some part of python script:

CONTEXT_SETTINGS = """
    {
    "CustomerTable": {
        "table": "%s",
        "host" : "localhost",
    "customerList" : %s
        }
    }
    """
class CmdLineParser():
    def __init__(self, description):
        self.parser = argparse.ArgumentParser(
            description=description)

    self.add_argument('-v', '--verbose', default=False,
                      action='store_true',
                      help='Verbose mode')   
    self.add_argument('-t', '--table', nargs='?',
                     help='my table', required=True)

    def add_argument(self, *args, **kwargs):
        self.parser.add_argument(*args, **kwargs)

    def get_ctx_config(self, cust_sub_ids):
        args = self.parser.parse_args()

        context_dict = json.loads(
            CONTEXT_SETTINGS % (args.table, customerids))
        return context_dict

how to load json file to set the customerids. where customerids = ['1234', '2345', '5678' .....] and get_ctx_config is call from main file.

so that input json file should become

{
"CustomerTable": {
    "table": "mytable",
    "host" : "localhost",
    "customerList" : ["1234", 2345", "5678"]          
  }
}

and context_dic return should like:

 u'CustomerTable': {u'table': u'mytable', u'host': u'localhost', u'cust_sub_id': [u'1234', u'1235', u'1236', u'1237', u'1234']}

how to write python script to update the.

 context_dict = json.loads(
         CONTEXT_SETTINGS % (args.table, customerids))

and what will be needed to set type for list in CONTEXT_SETTING like %s for String.

1
  • 3
    Maybe I'm missing something, but why are you storing your data structure in a string to begin with? Why not just have CONTEXT_SETTINGS be a dictionary Commented Feb 19, 2016 at 17:52

1 Answer 1

3

Why use JSON when all you want is a dictionary?

context_dict = {
"CustomerTable": {
    "table": args.table,
    "host" : "localhost",
    "customerList" : customerids          
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

only for understanding. for e.g. it should look like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.