<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sai Kaustubh</title>
    <description>The latest articles on DEV Community by Sai Kaustubh (@kaustubh_10).</description>
    <link>https://dev.to/kaustubh_10</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F812663%2F98aa7ff1-a8e4-407c-9f42-c3921c5fff03.jpeg</url>
      <title>DEV Community: Sai Kaustubh</title>
      <link>https://dev.to/kaustubh_10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaustubh_10"/>
    <language>en</language>
    <item>
      <title>Strapi v4 flatten complex response structure.</title>
      <dc:creator>Sai Kaustubh</dc:creator>
      <pubDate>Thu, 10 Feb 2022 10:49:12 +0000</pubDate>
      <link>https://dev.to/kaustubh_10/strapi-v4-flatten-complex-respnse-structure-4d1o</link>
      <guid>https://dev.to/kaustubh_10/strapi-v4-flatten-complex-respnse-structure-4d1o</guid>
      <description>&lt;p&gt;As we know that the strapi version 4 returns a very complex response structure. With nested data and attributes properties. This gets very confusing and tough to manage if your working with a large set of collections. &lt;br&gt;
The repetitive response structure of the version 4, bugged me out to constantly add the data and the attributes property. So this is a function which flattens the response structure and helps you work with the version 3 of strapi without much workover.&lt;/p&gt;




&lt;p&gt;How to use it?&lt;br&gt;
1) import the function file.&lt;br&gt;
2) const flattenData = flattenObj({...data});&lt;/p&gt;

&lt;p&gt;&lt;u&gt;NOTE&lt;/u&gt;: The data in step 2 is the data that is return from the response of strapi.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const flattenObj = (data) =&amp;gt; {
    const isObject = (data) =&amp;gt;
        Object.prototype.toString.call(data) === "[object Object]";
    const isArray = (data) =&amp;gt;
        Object.prototype.toString.call(data) === "[object Array]";

    const flatten = (data) =&amp;gt; {
        if (!data.attributes) return data;

        return {
        id: data.id,
        ...data.attributes,
        };
    };

    if (isArray(data)) {
        return data.map((item) =&amp;gt; flattenObj(item));
    }

    if (isObject(data)) {
        if (isArray(data.data)) {
        data = [...data.data];
        } else if (isObject(data.data)) {
        data = flatten({ ...data.data });
        } else if (data.data === null) {
        data = null;
        } else {
        data = flatten(data);
        }

        for (const key in data) {
           data[key] = flattenObj(data[key]);
        }

        return data;
    }

    return data;
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>strapi</category>
      <category>strapiv4</category>
      <category>graphql</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
