JSON List content must be a "object"

瀏覽次數:95 次
跳到第一則未讀訊息

Hans

未讀,
2021年9月29日 清晨5:10:162021/9/29
收件者: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

未讀,
2021年9月29日 下午4:49:462021/9/29
收件者:'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

未讀,
2021年9月30日 凌晨12:13:362021/9/30
收件者: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

未讀,
2021年9月30日 凌晨4:32:032021/9/30
收件者: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:
回覆所有人
回覆作者
轉寄
0 則新訊息