I am looking to modify the JSON format of the API response

Received this response from the API and need to convert the JSON format below:

Original:

"data": [
    [
      {
        "isFile": "true",
        "fileType": "image",
        "filesize": 100793,
        "filename": "attachment_0.png",
      }
    ],
    [
      {
        "isFile": "true",
        "fileType": "image",
        "filesize": 6343078,
        "filename": "attachment_1.png"
      }
    ]
  ]

Modified:

"data": [
      {
        "isFile": "true",
        "fileType": "image",
        "filesize": 100793,
        "filename": "attachment_0.png",
      },
      {
        "isFile": "true",
        "fileType": "image",
        "filesize": 6343078,
        "filename": "attachment_1.png"
      }
  ]

What is the best way to eliminate the array within the object.

Answer №1

  1. Transform JSON into JavaScript format
  2. Flatten out the array structure
  3. Convert JavaScript back to JSON
const json = JSON.stringify(JSON.parse(data).flat())

Adjustments made to JSON:

const data = `[
    [
      {
        "isFile": "true",
        "fileType": "image",
        "filesize": 100793,
        "filename": "attachment_0.png"
      }
    ],
    [
      {
        "isFile": "true",
        "fileType": "image",
        "filesize": 6343078,
        "filename": "attachment_1.png"
      }
    ]
  ]`
  
const json = JSON.stringify(JSON.parse(data).flat());
console.log(json);

Answer №2

To access all the information you need, simply refer to the initial item within the data array.

result = JSON.parse(data);
result.data = result.data[0];

Answer №3

In case the result isn't particularly large, you can use the following approach:

const jsonData = JSON.parse(result);
for (let key in jsonData) {
    key = key[0];
}

For larger responses, you might want to check out this resource

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to bring in a `devDependency` into your code?

The Mobx DevTool's README instructs users to install it as a dev dependency, and then import it into their code. This approach raises concerns for me because devDependencies are typically reserved for tools used in the build process or managing end co ...

How can I use CSS to ensure that both cards and images are equal in size?

I am currently utilizing react, with Material-ui being used for the Cards. For the layout, I have implemented CSS Grid Layout for the Grid system. Presently, the structure looks like this: https://i.stack.imgur.com/0FDyY.png However, my desired outcome i ...

Error encountered: Application module "MyApp" not found

I have set up AngularJs and jQuery using RequireJs within a nodeJs framework. This is my main.js setup require.config({ paths: { angular: 'vendor/angular.min', bootstrap: 'vendor/twitter/bootstrap', jqu ...

Unable to send a POST request to http://localhost:5000/api/createuser

During my journey through a MERN stack project tutorial on YouTube, I've come across a roadblock in the middle of my progress. The issue at hand involves the incorporation of user registration, which is a recent addition to my project. Utilizing Thund ...

Converting XML data into the JSON format and accurately extracting the relevant values

Currently, I am dealing with an XML object that needs to be converted into JSON format for easier access to values. The structure of the XML object is provided below: <env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'> ...

Manipulating JSON data in JavaScript

Currently delving into learning JSON as required for work purposes... I am attempting to dynamically add and delete records to a JSON object... Can anyone shed some light on why I keep receiving an UNDEFINED message? Here is the code... Appreciate any as ...

Storing a table structure in a variable using JavaScript

Kindly review this arrangement: 1 | 2 | 3 | .... ---------------------------------------------------------- 2016 100 2000 500 2015 540 450 1200 2014 120 230 660 I am seeking a method ...

Transfer both FormData object as well as an extra parameter through an AJAX request

I have successfully sent a FormData object like this: var formData = new FormData(); formData.append('file', this.files[0]); $.ajax({ url: urlUploadProductsFile, type: 'POST', data: formData, cache: false, contentType: f ...

MVC5 Toggle Button for Instant Display and Concealment

I used to utilize this method in Web Form development by targeting the id and name of the input radio button. However, I am encountering difficulties implementing it in MVC5. Can someone kindly point out where I might be going wrong? Upon selecting a radi ...

Invoke an array in PHP

If I need to call an array from PHP into TypeScript in array format, how can I accomplish this? Below is my PHP code: $AccessQuery = "SELECT name, lastname, phone, email FROM user INNER JOIN access ON id ...

Utilizing Node Mailer and Sendgrid to send emails in an Angular MEAN stack project with the angular-fullstack framework generated by Yeoman

Trying to figure out the best location for the code responsible for sending an email through a contact form in my Angular App using angular-fullstack MEAN stack from Yeoman. I managed to implement the email sending logic in the app.js file on the server s ...

implement a Django for loop within the template by utilizing JavaScript

Is it possible to incorporate a {% for in %} loop and {{ variables }} in a Django template using JavaScript DOM (insertAdjacentText, or textContent) and dynamically load data from the views without refreshing the entire page? If so, can you please guide me ...

Retrieving the selected value from a dropdown menu without having to click on a

I'm facing an issue with 2 dropdown menus outside of an HTML table that is generated by PHP code. There's a submit button within this table, and here's how it's set up: <!doctype html> Select Year: ...

I attempted to use the Stripe API, but it seems to be non

I seem to be encountering an issue with the stripe.customers.create. My goal is to create a new Stripe customer using the email address of the current user, and then update my user with the generated customer.id. Despite the fact that stripe.customers.cre ...

Tips for implementing mixins in a Nuxt.js application using the nuxt-property-decorator package

Recently, I worked on a project where I utilized Nuxt.js with TypeScript as the language. In addition, I incorporated nuxt-property-decorator. I'm currently trying to grasp the concept of the 'mixins' property in the code snippet below: mi ...

Is there a way to have two SVG files displayed on a single HTML page at the same time

Currently, I am facing an issue with my graphs. I have a line graph and a boxplot, but the boxplot is appearing below the line graph when I want it to be next to it. Any suggestions on how I can achieve this layout? Thank you! I attempted to use 2 differe ...

Is there a method in bootstrap that reveals the elements with the hidden class?

Currently, I am loading data into a bootstrap table on my JSP. The table class is hidden at the moment. After successfully uploading the data into the bootstrap table, I have implemented the following code: $(function() { var table = $('#Table') ...

Concealing HTML content with a modal overlay

There are security concerns with my authentication overlay modal, especially for users familiar with CSS and HTML. Is there a way to hide the HTML behind the modal so it doesn't appear in the page source? The code below is just an example of a modal ...

When incorporating conditional statements into your code, make sure to utilize the tags <script> along with

I have some script tags as shown below: <script src="cordova-2.5.0.js"></script> <script src="js/jquery-1.7.2.min.js"></script> <script src="js/jquery.mobile-1.1.1.min.js"></script> <script src="js/jquery.xdomainajax ...

arranging an array according to the values in a separate array

Hello all, I am new to Python and seeking help with a problem. I have an initial array: initial_array = np.array([1, 6, 3, 4]) In addition, I have another array: value_array = np.array([10, 2, 3, 15]) I would like to create an output array by sorting the ...