Function result is returning escaped string

I’m writing a function to return a json payload. When I do so, it looks like this:

{
     "response": "{\"result\": false}"
}

What I’d like it to do is:

{
     "response": {"result": false}
}

The code I’m using is (and response is an output variable of type System.String:

response = JsonConvert.SerializeObject(new { result = false });

The result is the same in Postman and Swagger.

Am I missing something?

Don’t have an answer for you, just a thought.

Is there an assembly or service that Epicor has that has Json helpers that you could use to select the output as Json instead of a string? I have no idea, just something I thought of.

Playing around with it a bit, I think I understand what it’s doing. It’s returning a json object with the return parameter(s) and then the result as a string.

So basically the Json is setup as:

{
   "parameter0": "escaped string value",
   "parameter1": "escaped string value"
}

And then you take the results and JsonConvert.DeserializeObject to get the resulting JSON payload.

When I do a Deserialize of

"{\"result\": false}"

I get:

{"result": false}
3 Likes

I don’t think there’s a way.

You have to provide some real type to return. In the ideal scenario you could define a response parameter as an object. Because you used a string, and serialized an anonymous object to a string, the response will be shown as such. The response is serialized twice.

I just work around the limitation by deserializing twice where you consume it.

3 Likes

Yep, that’s just how it works.

1 Like

If it makes you feel any better I’m wracking my brain to see if there is a return type that would fit your situation better.

Why exactly do you have an issue with what’s returned?

We have an external system that currently makes multiple API calls and gets JSON payload responses. My suggestion to make it more upgrade friendly is to create a function and return the information from the single function call.

I don’t know the limitations of the other system (yet), so I was just visually comparing the differences and was trying to figure out why it was different.

I wish (Idea time?) Epicor would have an option for some kind of JSON payload response rather than just assigning it to a string.

You and me both.

You can return a DataSet:

{
    "response": {
        "ds": [
            {
                "result": false
            }
        ]
    }
}

You can also with the appropriate references, return a few other objects that are direct json.
It might take a little digging, but there might be one that fits closer if it’s that important to you.

1 Like

I can ask the dev team for the other system and see what they prefer. The DataSet may be their vote.

1 Like

An epicor function in its own returns a JSON payload so if you just set the output variable of your function to result : boolean then the response wil look like

{
  "result":false
}

Is that what you want?

No, the payload is going to be complex. This was just a simple example to ask my question.

Ah, yeah there’s no way to return an arbitrary object in an Epicor function (yet)

2 Likes

DataSet is the way to go then.

You can massage that thing into a lot.

Columns can be nested objects.

Warning, Newtonsoft won’t directly deserialize a dataset with nested objects, so you gotta do a little work on the deserialize side sometimes.

Gah, so close. I wish I could use a DataTable instead of a DataSet. Any idea if there’s a way to add a DLL to the Assemblies so I could use something custom or a generic DataTable?

no

Edit: I found a dirty hack, it’s not stable. sorry

tease GIF

3 Likes

snip

2 Likes

But does it use Reflection?
Jean Yoon Lol GIF by Kim's Convenience

3 Likes

Nope!

1 Like