HttpPost to API using Dictionary

postDictionary is being compiled from two separate lists using zip in a for loop.
Result is:
{‘Barcode1’: ‘0190076338816517320100078911220322213609008158’,
‘Barcode2’: ‘0190076338816517320100079111220322213609008157’, }

First question, is there a way to make the Key the same and treat each as a separate key/value pair?

Ie. {“Barcode”: “013…”} , {“Barcode”: “014…”}


I setup the Keys list to take the string “Barcode” + str(index) for the number of cases generated. (the index runs in a range of 1-51, unless the barcode is == “99999”) When I send it to the API it’s rejected because of the attached number. ie. “Barcode1” instead of “Barcode”.

API is looking for this structure


{
  "Pallet": {
    "PalletNumber": "string",
    "Cases": [
      {
        "Barcode": "string"
      }
    ]
  }
}
This works when posting through the API web interface
      {
        "Barcode": "string"
      }
      ,
      {
        "Barcode": "string"
      }

Scripting to get everything in place

if palletStatus == "OK":
	palletFormat = {"Pallet": {"PalletNumber": apiPalletNumber, "Cases": [postDictionary]}}
	palletFormatJsonEncoded = system.util.jsonEncode(palletFormat)

This is after Json encoding;
{
   "Pallet":{
      "PalletNumber":"example 20 digits",
      "Cases": [{"Barcode1":"example 46 digits",
                       "Barcode3":"example 46 digits",
                       "Barcode2":"example 46 digits",
                       "Barcode4":"example 46 digits"
                      }]
                     }
               }

Any help would be greatly appreciated and sorry if this is a bit incoherent. If any clarification is needed, please let me know. The version of Ignition is 7.9 and all of this is being tested through the designers script console at the moment, but will later be added as a gateway script that’s triggered by a plc condition. Initially, I did create this using a for loop and httpPost for each iteration, but instead of a possible 50 different messages being sent at a time we are trying for 1 message with 50 records.

First, edit your post. Highlight all of your code (just the code). Click the “preformatted text” button. Save the edited post.

Then we can read your code.

pturmel I’m sorry, I didn’t see a “preview window.” I was in the process of trying to tone it down… as soon as that monstrosity posted I was looking for an edit button. :slight_smile:

What's in those lists ?

right now listA is the string “Barcode” + a dynamic value from the index ie. “Barcode1” … “Barcode2”
listB is a (for each case) 46 digit barcode that’s been converted to a string (pulled from the PLC)

print postDictionary would return ie. “Barcode1”:“0190076338212005320100074111220323213610001162”

Why are you using a list with "Barcode" + dynamic value if you only want “Barcode” ?

barcodes = ["129083", "5123454", "348957", "1237896"]
barcodes = [
	{
		'Barcode': bc
	} for bc in barcodes
]
#[
#  {'Barcode': '129083'}
#  {'Barcode': '5123454'}
#  {'Barcode': '348957'}
#  {'Barcode': '1237896'}
#]

Ignorance. I just started working with python, json formats, and APIs.
I wasn’t sure how to pass in one key with multiple values into a dictionary. When I tried I had a mess, so my initial thought was to add an identifier to make each key unique. (not realizing that the json structure would look specifically for the string “Barcode” and anything else would not work.

The swagger API structure looks like this:
{ "Pallet": { "PalletNumber": "String", "Cases": [ { "Barcode": "String" } ] } }

Well then that should solve your problem.
What’s question 2 ?

Not certain but, pretty sure dictionary comprehensions won’t work in 7.9.

May need to refactor to:

barcodes = ["129083", "5123454", "348957", "1237896"]
tBarcodes = []
for bc in barcodes:
	tBarcodes.append({'Barcode':bc})
barcodes = tBarcodes

Could be completely wrong though, don’t have 7.9 to trial it on.

It’s not a dictionary comprehension, it’s a list comprehension that makes a list of dictionaries. No reason it shouldn’t work.

Can I send you a PM?

Yea sure.