Seite 1 von 1

Export in JSON

Verfasst: 05.07.2019 14:30
von Velz
Hallo Profis.. ich muss zur Datenübergabe aus der Datenbank einen Export in ein JSON File programmieren. Ich habe aber noch nie etwas mit JSON gemacht und die Hilfe hat mich dem Thema noch nicht wirklich näher gebracht.
Hat jemand einen Beispielcode auf den ich aufbauen kann?

Es geht darum einen Kontakt mit Adresse zu exprtieren der mehrere Ansprechpartner und mehrere Historieneinträge hat. Also 1 zu n!

Beispiel:

Code: Alles auswählen

{
    "account": {
      "name": "firma gmbh",
      "street": "Stiefelstr. 211",
      "postalcode": 12345,
      "city": "Werne",
      "branche": "automotive",
      "phone": 012345-987456,
      "employees": 231,
      "revenue": 1234,
      "web": "http://www.firma-gmbh.com",
      "customtags": {
        "hasinternalxyz": "ja",
      }
    },
    "currentstate": {
      "state": "B - Anruf generic",
      "type": "WZW"
    },
    "contacts": [
      {
        "salutaion": "Herr",
        "firstname": "erwin",
        "lastname": "müller",
        "position": "Enticklungsleiter",
        "phone": "+49 12345 9874563"
      },
{
        "salutaion": "Herr",
        "firstname": "fritz",
        "lastname": "meier",
        "position": "putzfrau",
        "phone": "+49 12345 986523"
      }
    ],
    "history": [
      {
        "date": "27.09.2018",
        "user": "franz",
        "description": "das war so oder so...."
      },
      {
        "date": "26.09.2018",
        "type": "Infoversand",
        "user": "uli",
        "description": "Herr Meier ist dann so oder so..."
      }
    ]
  }

Re: Export in JSON

Verfasst: 05.07.2019 14:43
von Kiffi
JSON ist letztendlich nur ein String.

Sprich: Du könntest Deinen JSON-Export alternativ auch mit einer klassischen String-Verknüpfung realisieren.

Re: Export in JSON

Verfasst: 05.07.2019 14:52
von Velz
Angenommen ich mache es einfach als TextFile, reicht es dann die unten aufgeführten Escapes zu machen oder fällt mir dann noch mehr auf die Füße???

JSON String Escape / Unescape

Escapes or unescapes a JSON string removing traces of offending characters that could prevent parsing.

The following characters are reserved in JSON and must be properly escaped to be used in strings:

Backspace is replaced with \b
Form feed is replaced with \f
Newline is replaced with \n
Carriage return is replaced with \r
Tab is replaced with \t
Double quote is replaced with \"
Backslash is replaced with \\

Re: Export in JSON

Verfasst: 05.07.2019 15:03
von NicTheQuick
Hier mal ein angefangenes Beispiel:

Code: Alles auswählen

CreateJSON(1)

person = SetJSONObject(JSONValue(1))

account = SetJSONObject(AddJSONMember(person, "account"))
SetJSONString(AddJSONMember(account, "name"), "firma gmbh")
SetJSONString(AddJSONMember(account, "street"), "Stiefelstr. 211")
;...
customtags = SetJSONObject(AddJSONMember(account, "customtags"))
SetJSONString(AddJSONMember(customtags, "hasinternalxyz"), "ja")

currentstate = SetJSONObject(AddJSONMember(person, "currentstate"))
SetJSONString(AddJSONMember(currentstate, "state"), "B - Anruf generic")
SetJSONString(AddJSONMember(currentstate, "type"), "WZW")

Debug ComposeJSON(1, #PB_JSON_PrettyPrint)
Es geht also auch mit der JSOn-Library, es ist nur verdammt umständlich.

Re: Export in JSON

Verfasst: 05.07.2019 15:15
von Kiffi
Velz hat geschrieben:Angenommen ich mache es einfach als TextFile, reicht es dann die unten aufgeführten Escapes zu machen oder fällt mir dann noch mehr auf die Füße???
prinzipiell reicht das. Weiß nicht, welche Daten in Deiner DB sind.

Ein anderer Ansatz: Erstelle Dir eine PB-Struktur, befülle sie mit den Daten Deiner DB und führe danach ein InsertJSONStructure() aus.

Re: Export in JSON

Verfasst: 05.07.2019 15:22
von mk-soft
Habe mal angefangen zu basteln...

Fast schon richtig !?

Code: Alles auswählen

;-TOP

Structure udtCustomtags
  hasinternalxyz.s
EndStructure

Structure udtCurrentstate
  State.s
  Type.s
EndStructure

Structure udtContacts
  salutaion.s
  firstname.s
  lastname.s
  position.s
  phone.s
EndStructure

Structure udtHistory
  date.s
  user.s
  description.s
EndStructure

Structure udtAccount
  name.s
  street.s
  postalcode.s
  city.s
  branche.s
  phone.s
  employees.s
  revenue.s
  web.s
  customtags.udtCustomtags
  currentstate.udtCurrentstate
  List contacts.udtContacts()
  List history.udtHistory()
EndStructure

Structure udtData
  account.udtAccount
EndStructure

Global dat1.udtData
AddElement(dat1\account\contacts())
AddElement(dat1\account\history())

Debug "Test Structure"
If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), dat1, udtData)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf


Debug "Parse ..."
json.s = PeekS(?acount_data, -1, #PB_UTF8)
Debug json
ParseJSON(0, json)
Debug JSONErrorMessage()

Debug "Test Extract to Structure"
ExtractJSONStructure(JSONValue(0), dat1, udtData)
Debug JSONErrorMessage()
CallDebugger
  

DataSection
  acount_data:
  IncludeBinary "json.txt"
  Data.i 0
EndDataSection

Re: Export in JSON

Verfasst: 05.07.2019 15:44
von Velz
Vielen Dank schon mal... das werde ich am WE mal testen...