FoxPro Programming
json to a vfp cursor
Gravatar is a globally recognized avatar based on your email address. json to a vfp cursor
  Marco Antonio Sepulveda Valbuena
  All
  Jan 12, 2024 @ 05:39am

Hello, I need to convert a json to a vfp cursor, can you help me?

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Marco Antonio Sepulveda Valbuena
  Tore Bleken
  Jan 12, 2024 @ 10:37am

I am trying to follow the steps indicated, as shown in the code:

	FUNCTION enviar_factura
		LPARAMETER lofactura
SET STEP ON
		DO wwJsonSerializer
		loSer = CREATEOBJECT("wwJsonSerializer")
		lofactura1 = loSer.Serialize(lofactura)
		lofactura2 = loSer.DeserializeJson(lofactura1)
		
		CREATE CURSOR lofactura3 (number c(20), prefix c(4), type_document_id c(2))
		
		CollectionToCursor(lofactura2,"lofactura3")

but I get the following error:

"Property LOCOLLECTION is not found."

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Tore Bleken
  Marco Antonio Sepulveda Valbuena
  Jan 12, 2024 @ 11:06am

On which line? And what's the command on that line?

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Marco Antonio Sepulveda Valbuena
  Tore Bleken
  Jan 12, 2024 @ 12:41pm

On the highlighted line

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Rick Strahl
  Marco Antonio Sepulveda Valbuena
  Jan 12, 2024 @ 03:01pm

What are you serializing? You need to check the values of the things along the way. Use the debugger to check what the JSON is, what the deserialized object is (it's likely null based on the result you're seeing and thereform the JSON is probably null or not what you think it is).

Garbage in, Garbage out.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Marco Antonio Sepulveda Valbuena
  Rick Strahl
  Jan 15, 2024 @ 08:09pm

Next I show the images of the serialized and unserialized object in the debugger. For me everything is fine, I can't find the error, I have already thought about this case a lot.

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Rick Strahl
  Marco Antonio Sepulveda Valbuena
  Jan 15, 2024 @ 08:39pm

I'm not sure what you're trying to do, but it's unlikely to work by trying to read the data into a cursor via serialization...

You are starting with an object not a cursor and you're serializing that into JSON string. When you deserialize you end up with the same nested object not a cursor or array/collection. And why would you want to? You already have the data in an object that you can read the data out of. But it's not a list of any sort but a single object so why do you need a cursor?

If for some reason you actually need the data in a table, you can pull the data out of the object in memory and manually assign the values to the columns in the table.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Marco Antonio Sepulveda Valbuena
  Rick Strahl
  Jan 17, 2024 @ 12:28pm

I am calling an api passing it a json file, as I see the function receives an object, I need to convert that object into a cursor, I have not been able to go through the elements of that object, doing it with "for each item in locollection foxobject" the error that I already published, I have tried to read it in other ways but it has not been possible either.

I tried to do it only with "GATHER NAME lofactura", but the difficulty is that lofactura has other objects, so I don't know how many records I should add to the cursor to recover all the information of the object.

I appreciate your help.

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Rick Strahl
  Marco Antonio Sepulveda Valbuena
  Jan 17, 2024 @ 12:52pm

That's because you're not working with a collection or array. You are working with an object to begin with - there's nothing that you can automatically convert into a cursor.

loResult = loSer.Deserialize(lcJson)  && result is a JSON **OBJECT**

? loResult.Customer.Address
? loResult.date  && string

loLineItems = loResult.Invoice_Lines
FOR x = 0 to loLineItems.Count
    loItem = loLineItems.Item(x)
    ? "  " + loItem.Description
ENDFOR

You can potentially use CollectionToCursor() manually to parse the lineitems into a cursor.

Please - take the time to review the documentation - it explains how JSON is mapped to FoxPro and how you can parse.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Marco Antonio Sepulveda Valbuena
  Rick Strahl
  Jan 18, 2024 @ 05:04am

When using "CollectionToCursor()", I get the error I described above, "Property LOCOLLECTION is not found".

I need to convert that object that I am receiving into a cursor, since the methods that exist do not allow me to read that object, I am going to make my own method that reads the json and converts it into a cursor, unless I have another idea that can help me .

Thank you very much for all your support.

Gravatar is a globally recognized avatar based on your email address. re: json to a vfp cursor
  Rick Strahl
  Marco Antonio Sepulveda Valbuena
  Jan 18, 2024 @ 12:08pm

LOL!

I just gave you a way to parse the JSON with code, and you're going to write your own parser just so you can 'read it into a cursor'?

Okey dokey...

+++ Rick ---

© 1996-2024