ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

DATA-INTO variable mapping

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • DATA-INTO variable mapping

    Hello,

    I'm using DATA-INTO Op-code and it works great!


    Unfortunately I'm now facing a scenario where I am unable to use DATA-INTO due a couple of issues:

    1. variable name in JSON file begins with a _ ("_id").

    adding _id field to my data structure determines compilation errors.

    {
    "orderID": "22276161_6197b1eaa98e9",
    "billing": {
    "_id": {
    "$id": "6197b1e25fb8e0980c824ba4"
    }...

    2 Items list is not an array , but "dynamic" tags i.e. ."22609963""22566687","22524244"

    I don't know how to define or manage these DS with dynamic names
    ...
    "cart": {
    "total": 145.8,
    "items": {
    "22609963": {
    "count": 1,
    "price": 24.5,,
    "title": "Prosecco Superiore DOCG ",
    "sku": "SABX",

    },
    "22566687": {
    "count": 1,
    "price": 26.7,
    "title": "Set Trio ",
    "sku": "SETX",

    },
    "22524244": {
    "count": 2,
    "price": 15.2,
    "title": "Colli Trevigiani IGT ",
    "sku": "TRX",

    } ....

    The json is generated from an external E-Commerce application.
    Any help is really appreciated.

    Giovanni

  • #2
    Just a thought.
    I haven't tried using DATA-INTO yet but I thought that you could create a program that modifies the JSON-file you received.
    Reading the streamfile and when "_id": is found translate it to "my_id":

    In the same way translate the dynamic names to static tags and convert the dynamic names to a tag called itemnumber

    The result is saved in a new IFS file and this file is used as input to DATA-INTO

    The result should be something like this:

    {
    "orderID": "22276161_6197b1eaa98e9",
    "billing": {
    "my_id": {
    "$id": "6197b1e25fb8e0980c824ba4"
    }...


    ...
    "cart": {
    "total": 145.8,
    "items": {
    "item":{
    "itemnumber":22609963,
    "count": 1,
    "price": 24.5,,
    "title": "Prosecco Superiore DOCG ",
    "sku": "SABX",

    },
    "item":{
    "itemnumber":22566687,
    "count": 1,
    "price": 26.7,
    "title": "Set Trio ",
    "sku": "SETX",

    },
    "item":{
    "itemnumber":22524244,
    "count": 2,
    "price": 15.2,
    "title": "Colli Trevigiani IGT ",
    "sku": "TRX",

    } ....

    Comment


    • HMiller
      HMiller commented
      Editing a comment
      Thank you Peder for your reply.
      I did use suggestion posted by Jon and it works.

  • #3
    Your "_Id" issue is an easy one which DATA-INTO solves quite easily. Instead of coding the option case=any change to case=convert. This causes RPG to translate any illegal names to an RPG acceptable form. In this case it will simply cause the leading underscore to be dropped. so "_id" would become id.

    Your second problem is sadly not so easy and neither DATA-INTO nor the SQL JSON functions can deal with it as they rely on known names for mapping.

    Why people use this stupid style in JSON is hard to understand. It makes it much harder to process and all you save is a few characters in the data stream. But you also lose a validation option.

    Anyway - you can either process the entire document with the raw YAJL APIs (or use a Python, node, or PHP, or whatever routine) - I have used DATA-INTO for the bits that it could handle and then use YAJL's ability to drill down to specific items to handle the weird bits. I am in the process of writing up an RFE to have RPG handle these situations but don't hold your breath!

    Comment


    • HMiller
      HMiller commented
      Editing a comment
      Thank you Jon !
      It works using case=convert.
      I will try to use YAJL for the second problem.
      Thank you!
Working...
X