ORDA/Collections

2019-10-30 Thread Justin Will via 4D_Tech
I need a json object output in a particular format and 4D's new datastore and 
collections do this out of the box in 3 lines of code for me.  It's almost 
magical.

$oRecs:=ds.Complex.query("Domain_ID = :1";_Domain_ID)
$coll:=$oRecs.toCollection("ID, Name, Facilities.ID, Facilities.Name")
$vtReturn:=JSON Stringify($coll)

The problem I have is the Javascript libraries I'm using that requires the data 
needs the names of the JSON elements to be named in a particular way.  Is there 
a way to tell my collection rename ID to key and Name to label etc?

So 4D is currently outputting this.

[
{
"ID": 901,
"Name": "MP",
"Facilities": [
{
"ID": 1159,
"Name": "MP:North"
},
{
"ID": 1160,
"Name": "MP:South"
}
]
}
]



But what I need is this.

[
{
"key": 901,
"label": "MP",
"children": [
{
"key": 1159,
"label": "MP:North"
},
{
" key ": 1160,
"label": "MP:South"
}
]
}
]

I know I could just do a replace in string but this feels like the wrong way to 
go at this.

Thanks
Justin Will
**
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: ORDA/Collections

2019-10-30 Thread Kirk Brooks via 4D_Tech
Justin,
The replace string approach is not what you want to do. Editing strings and
mapping data are not the same thing. You also want to avoid hard coding
such maps. Any little change and you have to change code.

A fairly easy way to manage what you need to do is save an entity as a
stringified JSON. Open or paste this into your text editor.
Change the values for each field to the name you need to map it to. Don't
worry about data types or any of that.
Save this file in RESOURCES. Let's call it 'entityMap.json'

To use it:

$map_obj:=JSON Parse(Document to text())

$export_obj:=New object

For each($property;$entity_obj)

If($map[$property]=Null)  //  not mapped

$export_obj[$property]:=$entity_obj[$property]  //  use the existing name

Else

$export_obj[$map_obj[$property]]:=$entity_obj[$property]  //  use the name
you mapped it to

End if

End for each


//   do something with the export object


In this example I'm assuming the entity is a single 'level'. If you have
object fields you need to map as well you would put the testing part in a
method that takes a map and entity object as params. Then you could test
for object fields and pass the object recursively.

The code will work with any table because you have the actual map values
stored in JSON files.

Looking at the example you provided you are going to need to recognize when
you are exporting related entities. The basic idea is the same. I would
make handle each of the related entities a sub loop of the parent. It will
be more robust than including the relations in your parent entity map.

On Wed, Oct 30, 2019 at 1:45 PM Justin Will via 4D_Tech <
4d_tech@lists.4d.com> wrote:

> I need a json object output in a particular format and 4D's new datastore
> and collections do this out of the box in 3 lines of code for me.  It's
> almost magical.
>
> $oRecs:=ds.Complex.query("Domain_ID = :1";_Domain_ID)
> $coll:=$oRecs.toCollection("ID, Name, Facilities.ID, Facilities.Name")
> $vtReturn:=JSON Stringify($coll)
>
> The problem I have is the Javascript libraries I'm using that requires the
> data needs the names of the JSON elements to be named in a particular way.
> Is there a way to tell my collection rename ID to key and Name to label etc?
>
> So 4D is currently outputting this.
>
> [
> {
> "ID": 901,
> "Name": "MP",
> "Facilities": [
> {
> "ID": 1159,
> "Name": "MP:North"
> },
> {
> "ID": 1160,
> "Name": "MP:South"
> }
> ]
> }
> ]
>
>
>
> But what I need is this.
>
> [
> {
> "key": 901,
> "label": "MP",
> "children": [
> {
> "key": 1159,
> "label": "MP:North"
> },
> {
> " key ": 1160,
> "label": "MP:South"
> }
> ]
> }
> ]
>
> I know I could just do a replace in string but this feels like the wrong
> way to go at this.
>
> Thanks
> Justin Will
> **
> 4D Internet Users Group (4D iNUG)
> Archive:  http://lists.4d.com/archives.html
> Options: https://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
> **



-- 
Kirk Brooks
San Francisco, CA
===

What can be said, can be said clearly,
and what you can’t say, you should shut up about

*Wittgenstein and the Computer *
**
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: ORDA/Collections

2019-10-30 Thread Keisuke Miyako via 4D_Tech
rather than using ORDA,
you could use classic QUERY and call Selection to JSON with a template.

https://doc.4d.com/4Dv15/4D/15.6/Selection-to-JSON.301-3817892.en.html

the feature allows you rename and reorder your selection of fields however way 
you like.

> 2019/10/31 5:45、Justin Will via 4D_Tech <4d_tech@lists.4d.com>のメール:
>
> $oRecs:=ds.Complex.query("Domain_ID = :1";_Domain_ID)
> $coll:=$oRecs.toCollection("ID, Name, Facilities.ID, Facilities.Name")
> $vtReturn:=JSON Stringify($coll)




**
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

RE: ORDA/Collections

2019-10-30 Thread Justin Will via 4D_Tech
I couldn't seem to get Selection To JSON with a template to do what I wanted 
to.   I ended up writing this and it fits the bill.

For each ($oRec;$oRecs)
$oComplex:=New object
$oComplex.key:=$oRec.ID
$oComplex.label:=$oRec.Name
$oComplex.open:=True
ARRAY OBJECT($aoFacilities;0)
$oFacilities:=$oRecs.Facilities
For each ($oFac;$oFacilities)
C_OBJECT($oChild)
$oChild:=New object
$oChild.key:=$oFac.ID
$oChild.label:=$oFac.Name
APPEND TO ARRAY($aoFacilities;$oChild)
End for each 
OB SET ARRAY($oComplex;"children";$aoFacilities)
APPEND TO ARRAY($aoComplexs;$oComplex)
End for each 
$vtReturn:=JSON Stringify array($aoComplexs)

Thanks
Justin Will
**
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: ORDA/Collections

2019-10-30 Thread Keisuke Miyako via 4D_Tech
"For each" loop is a good place to start.

once you've familiarised yourself with collections and objects,
you might want to look into collection methods such as map() for this kind of 
work.

2019/10/31 9:11、Justin Will 
mailto:jw...@willwerks.com>>のメール:
I couldn't seem to get Selection To JSON with a template to do what I wanted to.



**
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: ORDA/Collections

2019-10-30 Thread Cannon Smith via 4D_Tech
Hi Justin,

Have you taken a look at the extract function on collections? I think it would 
do what you want.

--
Cannon.Smith
Synergy Farm Solutions Inc.
Aetna, AB Canada




> On Oct 30, 2019, at 2:45 PM, Justin Will via 4D_Tech <4d_tech@lists.4d.com> 
> wrote:
> 
> $oRecs:=ds.Complex.query("Domain_ID = :1";_Domain_ID)
> $coll:=$oRecs.toCollection("ID, Name, Facilities.ID, Facilities.Name")
> $vtReturn:=JSON Stringify($coll)
> 

**
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**