generating json from associative array

Started by Jeffrey Ferreira, July 29, 2025, 02:31:36 PM

Previous topic - Next topic

Jeffrey Ferreira

Hello,

I've generated JSON from an associative Array before. However, this next project i'm working on kind of requires an outer array (i.e. square brackets)...i'm not sure how to do that but the sample file looks like this:

[
   {"Order":"123456",
    "Customer":"ABC Co",
   }
   {"Order":"123457",
    "Customer":"XYZ Co",
   }
]

do i need to just build the inner JSON portions and then append the Square Brackets to beginning and end of the String before posting to web service?

thanks

jeff

James Zukowski

Basically, what you're doing is creating a JSON array. In PxPlus (v15.1 at least), an array has to have a name of some sort. In our experience, we've used "records". Then have an incrementing counter to identify the relative record within the array.

We send arrays with records that have a key value and a set of data values. Our associative array keys come out looking like: 
records.1.key_value
records.1.data.fieldn

and the result looks like:
{
  "records":[
    {
      "key_value":"12345",
      "data":{
        "fielda":"abcde",
        ...
        "fieldx":"xyz"
      }
    },
    {
      "key_value":"12346",
      "data":{
        "fielda":"abcdx",
        ...
        "fieldx":"qqq"
      }
    }
  ]
}

For your example, the keys may come out like:
records.1.Order
records.1.Customer
records.2.Order
records.2.Customer

with the output looking like:
{
  "records":[
    {
      "Order":"123456",
      "Customer":"ABC Co"
    },
    {
      "Order":"123457",
      "Customer":"XYZ Co"
    }
  ]
}

Hope this helps...
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Jeffrey Ferreira


Loren Doornek

Jeff, I've run into the same thing.  You need PXPlus version 17 or higher to automatically build the JSON in this format.  Since we have some clients using older versions, I've done as you suggested - build the inner JSON, then add the beginning and ending square brackets. 

Here's code that works on version 17, which generates the JSON as you expect:

j$["1.Order"]="123456"
j$["1.Customer"]="ABC Co"
j$["2.Order"]="123457"
j$["2.Customer"]="XYZ Co"
PRINT DIM(LIST EDIT J$)


[
  {
    "Order":"123456",
    "Customer":"ABC Co"
  },
  {
    "Order":"123457",
    "Customer":"XYZ Co"
  }
]


Also be aware that any response they send back may be encapsulated in the square brackets.  When I see that, I just strip them off, or replace the initial square brackets with "{data:[" and append a "}", so that the JSON is in format that earlier versions of PXPlus can work with.  Below is an example of doing that, using the same data as above.

print j$
[{"Order":"123456","Customer":"ABC Co"},{"Order":"123457","Customer":"XYZ Co"}]

dim load json$=j$ ! gets an error since the JSON starts with [
Error #17: Invalid file type or contents

jj$=sub(j$,"[","{data:[")+"}" ! convert the JSON to a new variable JJ$
dim load json$=jj$
print dim(list edit json$)

{
  "data":[
    {
      "Order":"123456",
      "Customer":"ABC Co"
    },
    {
      "Order":"123457",
      "Customer":"XYZ Co"
    }
  ]
}

Stéphane Devouard

Loren & Jeffrey

FYI, this type of JSON data is supported on PVX 16.20

Regards,
Stéphane Devouard
Portfolio | Work

Aaron Woodhouse

As a side note, you may also find good use of the JSON Object that was added in v22.00  :)
https://manual.pvxplus.com/?/utilities/obj_json.htm
Software Developer
PVX Plus Technologies LTD.