Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

njJson : not succeeding to get it work 3

Status
Not open for further replies.

Rajesh Karunakaran

Programmer
Sep 29, 2016
549
MU
Hi friends,

I downloaded the nfJson library from gitHub and the provided examples PRG files are working properly.

I have a json file, from which I want to retrieve data and populate a cursor. I have seen a PRG in nfJson 'nfopenjson.prg'. The example provided for that prg works fine. However, when I try to apply that to a slightly different structured json file, I am getting 'Syntax error' message.

Below is the content of the JSON file:

Code:
{
  "master" : {
    "decRef" : {
      "msgTyp" : "F",
      "prtofRptng" : "INNSA1",
      "jobNo" : 12345,
      "jobDt" : "20190806",
      "rptngEvent" : "SAM",
      "mnfstNoRotnNo" : 175912,
      "mnfstDtRotnDt" : "20190716",
      "vesselTypMvmt" : "FI"
    }
  }
}

Below is the program that I am trying:

Code:
lcJson = FILETOSTR('sampleValues.json')
=nfOpenJson(lcJson,'$.array', ' ;
		- msgTyp        v(1)  $.master.msgTyp ;
		- prtofRptng    v(20) $.master.prtofRptng')
BROWSE

I am just checking with retrieving only 2 columns. But, when I run this, I am getting 'Syntax error'.
Can anyone help me to find out the problem? What I am missing?

Thank you very much in advance,
Rajesh


 
Olaf / Marco,

In fact, I know the order in which the members appear should not be a dependency. But, I had a peculiar problem.

My client receives json file from someone else. Suppose we have data for 2 tables "mcref" and "cargodtl" in the json. In json file, "mcref" comes first and then the "cargodtl". These are related by the key field "IdNo". These are printed in a hierarchy way in json, ie for each "mcref" record there may be multiple "cargodtl" records. However, we have seen that, in the json, under "cargodtl" data, the "IdNo" field and value are not printed as a field:value pair. So, when I read the data of "cargodtl", obviously my "IdNo" field doesn't get populated. So, I have to update it with the IdNo value I read from "mcref". But then for this, the "mcref" should be read first to get the current "IdNo" and then the "cargodtl". But, as AMEMBERS made members in alphabetical order, my program was coming across the member "cargodtl" first and then there was no way to get the "IdNo" value from "mcref" (which has even not yet been read). That was the issue I am facing.

But, now I have solved it I believe. Checking the data to confirm.

Thanks
Rajesh
 
You could always create a set of cursors that don't have foreign key checks and populate them in any order, but whatever, it's good the problem is finally solved.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Olaf,

There are no foreign key checks of any kind. I was just not able to determine the key value from the master table that has to be written to the child one because the child was being read first before the master. Now, before start working on the member list, I am reordering the array of members to match the order in which they are supposed to be. And then, when I read, I get to read the master first and then the child. Then, I know the key value from the master that I have to write to the child table. Thus, the problem solved.

Thank you all for your help, guidelines and time
Rajesh
 
I see: You generate keys. Well, you still can read into a set of cursors with an NULL default column you then set as aftermath. You'll need to have some relationship to know which detail data will need the ID of a parent as foreign key.

In object hierarchies, the parent-child relationship is given by a child pointing to its parent. VFP objects have a parent property for that. So this form of data needs no key, still, you can store IDs in JSON, too. I thought, for example, in your sample JSON about shipItnry the shipItnrySeq could be such an id. Unusual with negative numbers, but why not? If that appears in other objects in the JSON you have such relationships already coming from the JSON, not generated by you.

Anyway, it doesn't matter, whether you generate them or they are in the data itself, by a property or by object hierarchy, you still never depend on a sort order of data as you can always load it into a "queue", your own data structure you need to finally apply it in the order you want.

You solved it with meta control data and that's okay, but it wasn't necessary and your only choice or possibility.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Marco,

Just a thought. If the json file is invalid for some reason, the nfJsonRead program generates an error.
Is there any way to trap this? If so, my calling program can handle it appropriately and alert the user about the situation.

Rajesh
 
Yes, with ON ERROR.

Marcos routines throw errors using the ERROR command and that means ERROR() is 1098 and the message is as the text Marco puts together.
The only a bit unfortunate thing about the ERROR command is, the lineno of error reported by LINENO() in your error handler is reported as the one with the ERROR statement.

In some way that's ok, but I had quarrels with developers pointing out the place of error is my routine...

The usual error of a native function that's not VFP code is the line of the caller, as in a clean tested working language you don't have errors in a function but places where you put much thought work in checking errors made by the caller, throwing an error exception to inform him about that.

And the other even bigger disadvantage is, that you don't get the chance to continue code execution after the erroneous call, you can only go back to the line inside the function after the ERROR or (what makes even less sense, let RETRY go back to the ERROR command.

Aside of that bad design of ERROR (just in my opinion - and to be clear that's MS VFP team fault, not Marco's), that's already included, Rajesh, you simply need to handle these errors in your error handling.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Besides, ERROR also works to throw you into the CATCH of a TRY CATCH. You can for example do:

Code:
Try
  oJSON = nfjsonread(cJSON)
Catch to loException as Exception
  ? loException.ErrorNo, loException.Message
Endtry

So you have all methods of error handling with those errors reported by Marcos nfJSON programs.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Olaf,

I know TRY...CATCH. But, for this one, I have never thought in that way.
A TRY CATCH routine should do it decently.

Thanks
Rajesh
 
Hi Team!

Thank you so much everyone for your time and fantastic suggestions!
I got a bit late to come back here and say this, though.

Now, my writing and reading programs work the way I wanted it.
Some tweaks are still required but that's not related to nfJson or the core process.

with lot of proud, being here :)
Rajesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top