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!

nfOpenJson to create Cursor 1

Status
Not open for further replies.
If you have nested arrays, you'll need a 2 step process.

nfOpenJson is good at flatten object structures, and I made it as an early "sql server syntax compatibilty" exercise.
But as I state in the provided samples, I realized it's much better to just simply create the desired cursors beforehand
and populate using a simple loop.

Anyway let's make it 1st flattening the objects using nfOpenJson, and then the desired way:

Code:
TEXT TO books noshow
[{
  "Library": "Books",
  "Shelf": 5,
  "version": "LIB3.1.2",
  "hash": "hash",
  "Book": [
    {
      "Name": "Ths Blooms",
      "Category": "Fiction"
    },
    {
      "Name": "Legends",
      "Category": "Drama"
    }
  ]
},
{
  "Library": "Books",
  "Shelf": 6,
  "version": "LIB3.1.3",
  "hash": "hash",
  "Book": [
    {
      "Name": "Those Blooms 2",
      "Category": "Another Fiction"
    },
    {
      "Name": "Legends 2",
      "Category": "Drama 2"
    }
  ]
}
]
ENDTEXT


* just for illustration purposes how to flatten objects using nfOpenJson

nfOpenJson( m.books,'$.array',';
- Library c(15) $.Library;
- Shelf n(6) $.Shelf;
- version1 c(10) $.version ;
- Book1Name c(40) $.Book(1).name;
- Book1category c(40) $.Book(1).Category;
- Book2Name c(40) $.Book(2).Name;
- Book2Category c(10) $.book(2).category;
')


Browse

* desired way:


Create Cursor Library ( Library c(15), shelf N(6), Version c(10), hash c(10), Name c(20), category c(20))

jLibrary = nfJsonRead(m.books)

For Each oLibrary In jLibrary.Array

	For Each book In oLibrary.book

		Append Blank
		Gather Name oLibrary
		Gather Name book

	Endfor

Endfor

Browse




Marco Plaza
@nfoxProject
 
Thanks a lot Marco. You help is greatly appreciated and nfjson tool is also awesome.
 
Thanks. Though I just realized it had an error.

I edited the original post to this ( please check ):

Code:
Create Cursor Library ( Library c(15), shelf N(6), Version c(10), hash c(10), Name c(20), category c(20))

jLibrary = nfJsonRead(m.books)

For Each oLibrary In jLibrary.Array

	For Each book In oLibrary.book
		Append Blank
		Gather Name oLibrary
		Gather Name book
	Endfor

Endfor

Browse





Marco Plaza
@nfoxProject
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top