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(<RESOURCES/entityMap.json>))

$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 <
[email protected]> 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:[email protected]
> **********************************************************************



-- 
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:[email protected]
**********************************************************************

Reply via email to