JSON List content must be a "object"

95 views
Skip to first unread message

Hans

unread,
Sep 29, 2021, 5:10:16 AM9/29/21
to Bots Open Source EDI Translator
Hi All,

I receive a json file from a partner where some values are put into an array without a propertyname. for example "EANs" in the following json:

[
{
"Id": 12345,
"EANs": [
"1234567890123",
"2345678901234"
],
"Stock": 100,
"Properties": [
{
"Name": "Name",
"Value": "Value"
}
],
}
]


I try to create a grammar for it like:


structure = [
{ID:'LIN',MIN:0,MAX:99999,LEVEL:[
    {ID:'EANs',MIN:0,MAX:99999},
    {ID:'Properties',MIN:0,MAX:99999},
]},
]

recorddefs = {
'LIN':
    [
    ['BOTSID', 'M', 256, 'AN'],
    ['Id', 'C', 256, 'AN'],
    ['EANs', 'C', 256, 'AN'],
    ['Stock', 'C', 256, 'AN'],
    ['Properties', 'C', 256, 'AN'],
    ],
'EANs':
    [
    ['BOTSID', 'C', 256, 'AN'],
    ['BOTSCONTENT', 'C', 4096, 'AN'],
    ],
'Properties':
    [
    ['BOTSID', 'M', 256, 'AN'],
    ['Name','C',256,'AN'],
    ['Value','C',256,'AN'],
    ],
}

If i create a sublevel in the structure for "EANs", as the example above, i get the error: "ValueError: Expecting ':' delimiter: line 4 column 28 (char 47)"
because it's missing the 
If i remove the sublevel and just want to read "EANs" as a record, i get the error: "InMessageError: [J54]: List content must be a "object"."

Is there a way to tackle this issue?
Thanks, Hans

Eppye Bots

unread,
Sep 29, 2021, 4:49:46 PM9/29/21
to 'Chuck Turco' via Bots Open Source EDI Translator
bots does not support this now.

kind regards, Henk-Jan Ebbers


--
You received this message because you are subscribed to the Google Groups "Bots Open Source EDI Translator" group.
To unsubscribe from this group and stop receiving emails from it, send an email to botsmail+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/botsmail/30d58a0c-a436-4d20-90b4-b942b6211478n%40googlegroups.com.

Wassily

unread,
Sep 30, 2021, 12:13:36 AM9/30/21
to Bots Open Source EDI Translator
The way I handled this, was to convert the JSON to an XML first with the dicttoxml library, which then names all the elements "something_Item". Routescript as follows (including cleaning out some unwanted characters):

import os
import bots.preprocess as preprocess
from bots.botsconfig import *
import bots.botslib as botslib
import bots.botsglobal as botsglobal
import json
import dicttoxml
import re

LIST_ID_EXT = 'Item'


def postincommunication(routedict, *args, **kwargs):
''' function is called after the communication in the route.'''
preprocess.preprocess(routedict=routedict,function=make_xml)


def make_xml(ta_from, endstatus, *args, **kwargs):
try:
my_item_func = lambda x: x + '_' + LIST_ID_EXT
# copy ta for preprocessing
ta_to = ta_from.copyta(status=endstatus)
# open the files
content = botslib.readdata(filename=ta_from.filename)
obj = json.loads(content)
xml = dicttoxml.dicttoxml(
obj,
attr_type=False,
custom_root='SomethingHere',
item_func=my_item_func)
xml_cleaned = re.sub(u'[^\n\r\t\x20-\x7f]+',u'',xml)
# copy to next transaction
destfile = botslib.abspathdata(str(ta_to.idta))
botslib.dirshouldbethere(os.path.dirname(destfile))
with open(destfile, 'w') as f:
f.write(xml_cleaned.encode('utf-8').strip())
f.close()
# update outmessage transaction with ta_info;
ta_to.update(statust=OK, filename=str(ta_to.idta))
except Exception as exc:
txt = botslib.txtexc()
botsglobal.logger.error(u'Custom preprocess failed. Error:\n%s', txt)
raise botslib.InMessageError(
u'Custom preprocess failed. Error:\n$error', error=txt)

Hans

unread,
Sep 30, 2021, 4:32:03 AM9/30/21
to Bots Open Source EDI Translator
Hi Wassily, 

Thanks for tip, however the partner allready has a xml version of their stockfeed, but they insert some html content which messes up the file.
if i convert the json variant to xml i'll get the same issue :)
I guess i have to find out how to decrappify their html infested XML file then...
Op donderdag 30 september 2021 om 06:13:36 UTC+2 schreef Wassily:
Reply all
Reply to author
Forward
0 new messages