/    Sign up×
Community /Pin to ProfileBookmark

How to transform API response into particular format ?

I am getting below response. So, from the response we need to remove “_text” whenever it’s present in any object property. In some cases , i am also receiving key as an array which is causing problem to transform the data with current logic.

[code]
actualResponse = [
{
type: {
_text: “user”,
},
data: {
desc: {
_text: “abbc”,
},
desc1: {
_text: “jhd”,
},
},
data1: {
clients: {
client: [
{
clientName: {
_text: “Test 111”,
},
date: {
_text: “02/02/2020”,
},
},
{
clientName: {
_text: “Test 222”,
},
date: {
_text: “01/02/2020”,
},
},
],
},
},
msg: {},
id: {
_text: “123”,
},
},
{
type: {
_text: “student”,
},
data: {
desc: {
_text: “dgfg”,
},
desc1: {
_text: “jgjg”,
},
},
data1: {
clients: {
client: [
{
clientName: {
_text: “Test 333”,
},
date: {
_text: “02/02/2020”,
},
},
{
clientName: {
_text: “Test 444”,
},
date: {
_text: “01/02/2020”,
},
},
],
},
},
msg: {},
id: {
_text: “134”,
},
},
];

[/code]

Now, i wanted to remove the ._text from response.

[code]
expectedResponse = [
{
type: “user”,
data: {
desc: “abbc”,
desc1: “jhd”,
},
msg: {},
id: “123”,
data1: {
clients: {
client: [
{
clientName: “Test 111”,
date: “02/02/2020”,
},
{
clientName: “Test 222”,
date: “01/02/2020”,
},
],
},
},
},
{
type: “student”,
data: {
desc: “dgfg”,
desc1: “jgjg”,
},
msg: {},
id: “134”,
data1: {
clients: {
client: [
{
clientName: “Test 333”,
date: “02/02/2020”,
},
{
clientName: “Test 444”,
date: “01/02/2020”,
},
],
},
},
},
];

[/code]

I use the below logic to convert but it failed to convert data1 property from actual response. Instead it end up in infinite -loop. So i had to modify the below code

[code]
function convObj(ob) {
for (key in ob) {
if (ob[key]._text) {
ob[key] = ob[key]._text;
} else {
if(key === “0”) {
break; // modified logic because key is coming as an array but it failed to transform
}
convObj(ob[key]);
}
}
}
actualResponse.forEach(item => {
convObj(item);
});
console.log(actualResponse);
[/code]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 04.2021 — Why open a new thread instead of continuing in your previous one? This is the same subject.

Unfortunately the actualResponse you posted in this thread works fine for me with the original version of convObj() (without a break). No crash or stack overflow. Does you original response have a deeper nesting than this one?

I propose that you test the actualResponse you posted here again without a break and check if it works for you.

I suspect that your original response is larger than this one.
Copy linkTweet thisAlerts:
@vishalvickram1991authorFeb 04.2021 — @Sempervivum#1627562 Actual response worked with that logic. But not work with current actual response, it ended up in infinite loop. So, i have to make break at least to work for some level but whenever key as array index it's stop working
Copy linkTweet thisAlerts:
@vishalvickram1991authorFeb 04.2021 — @Sempervivum#1627562 Yeah correct this actual response again working fine with your current logic. But the below actual response its' getting failed.

Can you please try with below actual response ?
<i>
</i>actualResponse = [
{
type: {
_text: "user",
},
data: {
desc: {
_text: "abbc",
},
desc1: {
_text: "jhd",
},
},
message: {
langData: {
link: {
url: "e",
value: "el",
},
},
},
data1: {
clients: {
client: [
{
clientName: {
_text: "Test 111",
},
date: {
_text: "02/02/2020",
},
},
{
clientName: {
_text: "Test 222",
},
date: {
_text: "01/02/2020",
},
},
],
},
},
msg: {},
id: {
_text: "123",
},
},
{
type: {
_text: "student",
},
data: {
desc: {
_text: "dgfg",
},
desc1: {
_text: "jgjg",
},
},
message: {
langData: {
link: {
url: "e",
value: "el",
},
},
},
data1: {
clients: {
client: [
{
clientName: {
_text: "Test 333",
},
date: {
_text: "02/02/2020",
},
},
{
clientName: {
_text: "Test 444",
},
date: {
_text: "01/02/2020",
},
},
],
},
},
msg: {},
id: {
_text: "134",
},
},
];



function convObj(ob) {
for (let key in ob) {
if (ob[key]._text) {
ob[key] = ob[key]._text;
} else {
convObj(ob[key]);
}
}
}
actualResponse.forEach(item =&gt; {
convObj(item);
});
console.log(actualResponse);
Copy linkTweet thisAlerts:
@SempervivumFeb 04.2021 — Try this code, it works for me without issues:
function convObj(ob) {
console.log(ob);
for (let key in ob) {
if (ob[key]._text) {
ob[key] = ob[key]._text;
} else {
if (typeof ob[key] == 'object') {
convObj(ob[key]);
}
}
}
}
// actualResponse.forEach(item =&gt; {
// convObj(item);
// });
convObj(actualResponse);
Copy linkTweet thisAlerts:
@vishalvickram1991authorFeb 04.2021 — @Sempervivum#1627570 It's worked. Thanks a lot
Copy linkTweet thisAlerts:
@SempervivumFeb 04.2021 — Fine, you're welcome!
Copy linkTweet thisAlerts:
@vishalvickram1991authorFeb 05.2021 — @Sempervivum#1627595 I am slightly confused. Can you please explain me the real issue why your previous logic didn't work ?

I know in some level we are not getting ._text key , it might happen due to that one
Copy linkTweet thisAlerts:
@SempervivumFeb 05.2021 — In order to clarify the reason for the endless loop I restored the faulty version of the function and added some logging:
function convObj(ob) {
for (let key in ob) {
console.log(typeof ob, ob, key, typeof ob[key], ob[key]);
if (ob[key]._text) {
ob[key] = ob[key]._text;
} else {
// if (typeof ob[key] == 'object') {
convObj(ob[key]);
// }
}
}
}

The resulting output is:
object {url: "e", value: "el"} url string e
zz-test21.html:147 string e 0 string e
zz-test21.html:147 string e 0 string e
zz-test21.html:147 string e 0 string e
zz-test21.html:147 string e 0 string e
zz-test21.html:147 string e 0 string e
zz-test21.html:147 string e 0 string e
When processing the string "e" the object and it's first element are equal.

First element of "e" in for loop is "e"

Call convObj with parameter "e"

First element of "e" in for loop is "e"

Call convObj with parameter "e"

which is an endless loop.

You wrote:
>I know in some level we are not getting ._text key , it might happen due to that one

Does this mean that there is still an issue in the latest version which is running without an endless loop?
Copy linkTweet thisAlerts:
@VITSUSAFeb 05.2021 — @vishalvickram1991#1627611 Once compare both codes then you can easily understand about exact problem.
×

Success!

Help @vishalvickram1991 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...