Parse args to json elements in python
<p>i have a data structure that i want to fill with cmd args</p>

<pre><code>params = {
  ...
  ...
  "blockX": "{\"nameX\":["\Arg1\","\Arg2\",\"Arg3\"........"\ArgX\"]}",
  ...
  ...
}
</code></pre>

<p>Then i run a post request with the params as data:</p>

<pre><code>r = requests.post("https://url", data=params)
</code></pre>

<p>I only know how to do it with one element</p>

<pre><code>"BlockX": "{\"nameX\": \"%s\"}" %ArgX,
</code></pre>

<p>Regards</p>

<p>Your example suggest you want to <code>join</code> the arguments. You can convert any list into a single string by merging with a separator:</p>

<pre><code>args = ['foo', 'bar', 'hold the mustard']
print('", "'.join(args))
</code></pre>

<p>So to get your list <code>'"Arg1", "Arg2"'</code> etc, join them with the '", "' separator and insert <em>that</em> into your template:</p>

<pre><code>'{"nameX": ["%s"]}' % '", "'.join(args)
</code></pre>

<p>Note how <code>'</code> allows you to skip escaping each <code>"</code>. In your example, this would look like:</p>

<pre><code>params = {
  #...
  "blockX": '{"nameX": ["%s"]}' % '", "'.join(args),
  # ...
 }
</code></pre>

<p>Please keep in mind that you are putting a string representation of a JSON into a JSON! This is... perhaps not the right thing to do. A proper API would expect just a single JSON, in which case you can dump <code>args</code> directly.</p>

<pre><code>params = {
  #...
  "blockX": {
    "nameX": args
  },
  # ...
 }
</code></pre>

python json args