Transform JSON

Hi!

I have a question:

I create a Operation REST API , when I call to my client trough this REST API, my cliente returns list of object in JSON, like that:

[
    {
    "Center" : "aaaaaaaaaa",
    "Nif" : "00000000T",
    "NumberCenter" : "00000000",
    "Name" : "ppppp",
    },
    {
    "Center" : "aaaaaaaaaa",
    "Nif" : "00000000T",
    "NumberCenter" : "00000000",
    "Name" : "ppppp",
    },
    {
    "Center" : "aaaaaaaaaa",
    "Nif" : "00000000T",
    "NumberCenter" : "00000000",
    "Name" : "ppppp",
    }
]

On the other hand, I have in my Ensemble, Two class:

Class A --> lisf of class B

Class B --> Center, Nif,NumberCenter, Name

I need to iterate over JSON and capture de information a put this in my object A

I have been investigating... And I found a approximation that i want to do...

First of all, I want to iterate over JSON and capture de information

set a = "[{""Center"" : ""DKV"",""Nif"" :""00000001T"",""NumberCenter"" :""DKV_1"",""Name"" : ""Nombre_DKV""},{""Center"" : ""Vivisol"",""Nif"" : ""00000002T"",""NumberCenter"" : ""Vivisol_1"",""Name"" : ""Nombre_vivisol"",},{""Center"" : ""Stacks"",""Nif"" : ""00000003T"",""NumberCenter"" : ""Stacks_1"",""Name"" : ""Nombre_Stacks"",}]"
set JSON = ##class(%DynamicAbstractObject).%FromJSON(a) 
set iter = JSON.%GetIterator() 
while iter.%GetNext(.key,.value){write "Center = " _value.Center ,! }

can you give  me tips to make it?

  • 0
  • 0
  • 475
  • 7
  • 2

Answers

I would suggest that you working on frontend side in the browser, and call some REST API written in Caché or not, it does not matter. So, your code in JavaScript.

var data = getDateFromRest(); // you got some data from server, no so matter how you did it.

I suggest that your data is array, so, you could use map function and it contains something like this [{ "title": "car", "currency": "USD", "cost": "10000" }]

var newData = data.map(el => { return { name: el.title, value: el.cost + el.currency } })

will give you new array [{ "name": "car", "value": "10000USD" }]

 

I'm sorry I think that i'm not explain very good. I'll try to do better...

I create a Operation REST API , when I call to my client trough this REST API, my cliente returns list of object in JSON, like that:

[
    {
    "Center" : "aaaaaaaaaa",
    "Nif" : "00000000T",
    "NumberCenter" : "00000000",
    "Name" : "ppppp",
    },
    {
    "Center" : "aaaaaaaaaa",
    "Nif" : "00000000T",
    "NumberCenter" : "00000000",
    "Name" : "ppppp",
    },
    {
    "Center" : "aaaaaaaaaa",
    "Nif" : "00000000T",
    "NumberCenter" : "00000000",
    "Name" : "ppppp",
    }
]

On the other hand, I have in my Ensemble, Two class:

Class A --> lisf of class B

Class B --> Center, Nif,NumberCenter, Name

I need to iterate over JSON and capture de information a put this in my object A

So do you need to iterate over a Caché %Collection or a Javascript array?
Which one?

If you need to iterate over Javascript arrays WHILE transforming each item, it's just like Dmitry said.

for i=1:1:collection.Count() {
    set item = collection.GetAt(i)
    // From this point it depends of the type of item you're storing.
    // You must know to fetch each property values.
    // Ex: set value = item.property (%ZEN.proxyObject)
    // set value = item.GetAt("property") (%ArrayOfDataTypes)
  }

Change the values and reassign it to each related property.

do item.SetAt("property", newValue)
set item.property = newValue

Ok!

I have been investigating...

I found a approximation that i want to do...

First of all, I want to iterate over JSON and capture de information

set a = "[{""Center"" : ""DKV"",""Nif"" :""00000001T"",""NumberCenter"" :""DKV_1"",""Name"" : ""Nombre_DKV""},{""Center"" : ""Vivisol"",""Nif"" : ""00000002T"",""NumberCenter"" : ""Vivisol_1"",""Name"" : ""Nombre_vivisol"",},{""Center"" : ""Stacks"",""Nif"" : ""00000003T"",""NumberCenter"" : ""Stacks_1"",""Name"" : ""Nombre_Stacks"",}]"
set JSON = ##class(%DynamicAbstractObject).%FromJSON(a) 
set iter = JSON.%GetIterator() 
while iter.%GetNext(.key,.value){write "Center = " _value.Center ,! }

Second i want to put it in my object...  still investigating more about this

%DynamicAbstractObject instances have a method %Set(key, value) that you can use to insert new or update existing values.

set o = { "foo": "bar" }
write o.%ToJSON() // {"foo": "bar"}

do o.%Set("foo", "bars")
write o.%ToJSON() // {"foo": "bars"}

Comments

Next time, when you have a question, please create it from the main page, not as a comment to the post. In this case, you may get the response much quicker.

And to you question, on which side you have to do such transformation? Do you call some external REST API from Caché, or do you call some API written in Caché from your WebApplication which worked in Browser, or another way?

I call a external REST API and this returns a list of object in JSON