Is this a bug with Web.ToJSON and Web.FromJSON?

Hi there,

I have the following code:

result = CAPI.Exec( dbConnect.fConnection, 'select ID,Name from KUAF')

result2 = Web.FromJSON( Web.ToJSON( result ))

Obviously result is RecArray, however, result2 is list of Assoc!

Is it a bug? If not, what's the best way to cast list of array to RecArray?

Comments

  • This is not a bug. WebToJSON, creates an Array of Objects in response to a RecArray as that is the only way to represent a Recarray.

    Conversely, I you have an Array of Objects represented in JSON, the most obvious way to represent this isn OScript is as a List of Assocs.

    I'm assuming your example was just to explain the potential bug as you are starting with a recarray and trying to convert it back to a recarray. What do you actually need to do? If you're trying to convert a JSON Array of Objects to a Recarray you could convert it to the list of Assocs and then loop through using Assoc.FromAssoc to insert records into a new Recarray. There is also likely some clever string manipulation you could do if performance is an issue.

    Greg

  • You are exactly right, Greg. I need to give the JSON provider a spec of what I want in order to feed my other processes RecArray. I was hoping an array of objects in JSON became RecArray by Web.FromJSON. Looks like mapping it to list of Assoc in OScript is the only reasonable choice.

    I'm doing exactly what you said to convert it to RecArray, just realized there was no native conversion available in oscript.

  • The behavior you've described is likely not a bug. It seems that the result variable is a RecArray (record array), while result2 is being converted into a list of Assoc (associative arrays).

    A RecArray is a structured array with named fields, similar to a table or a spreadsheet, where each field can be accessed using its name. On the other hand, a list of Assoc is a list of associative arrays or dictionaries, where each element of the list is a dictionary-like structure with key-value pairs.

    It's possible that the Web.FromJSON function converts the RecArray result into a list of Assoc, which is a common representation for JSON data, as JSON doesn't natively support record arrays. The conversion might have been done for the sake of JSON compatibility.

    If you need to convert the list of Assoc result2 back into a RecArray, you'll need to have some information about the field names and their corresponding data types. Without knowing the exact structure of your data, it's difficult to provide specific code, but I can offer a general approach.

    Suppose you have information about the field names in a list called field_names, and you know the corresponding data types in a list called data_types. You can use the numpy.rec.fromarrays function (assuming you have NumPy installed) to convert the list of Assoc back to a RecArray.