Is there a way to modify the existing JSON format?

After receiving a JSON response, I used the code snippet

result = JSON.parse(result.value);
to parse it.

The JSON response I received was:

{"name":"For ","children":["{ \"name\":\"sxsm cnklsd\"}","{ \"name\":\"smd csdm\"}"]}
.

My goal is to convert this structure into the desired format:

{
    "name": "For  ",
    "children": [
        {
            "name": "sxsm cnklsd"
        },
        {
            "name": "smd csdm"
        }
    ]
}

I attempted double parsing, stringify, and then parsing again but nothing seemed to work. Any assistance would be greatly appreciated.

Answer №1

To begin, you should first parse the JSON in the same manner:

> data = JSON.parse(data.value);
< {"title":"Introduction","chapters":["{ \"title\":\"Chapter 1\"}","{ \"title\":\"Chapter 2\"}"]}
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^

Within the JSON object, there is an array called chapters that contains two JSON strings. Proceed to parse each one individually:

> data.chapters = data.chapters.map(JSON.parse)
> data
< {"title":"Introduction", "chapters":[{ "title":"Chapter 1"}, {"title":"Chapter 2"}]}

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

Incorporate a JavaScript variable within the src attribute of a script tag

Embedding Google's script allows for displaying a trends Map on our website. <script type="text/javascript" src="//www.google.com.pk/trends/embed.js?hl=en-US&q=iphone&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=500&a ...

HTTP Proxy: Discard partially completed requests

My Node.js-based HTTP Proxy has the ability to transform response body on the fly in a streaming fashion. Similar to nginx, my proxy compresses the response body using libz for gzipping. https://i.sstatic.net/VrgX7.jpg However, I am faced with a question: ...

Why is JSON decode returning null?

Is there a way to retrieve an array from the provided json data? $jsondata = "{ 'map' : {'center': {'lat': 24.68599, 'lng': 46.7026558}, 'zoom': 16}, 'marker': { 'position': {'l ...

ESLint has detected a potential race condition where the `user.registered` variable could be reassigned using an outdated value. This issue is flagged by the `require-atomic-updates` rule

I have developed an asynchronous function which looks like this: let saveUser = async function(user){ await Database.saveUser(user); if (!user.active) { user.active = true; //storedUs ...

Is it possible to create a clip plane in Three.js for objects that intersect?

Currently, my setup involves using a PlaneGeometry as the representation of water. I have also added a ship (gltf model) onto the water. The issue I'm encountering is that when the boat slightly overlaps with the water surface, the water is visible in ...

At what point in the lifecycle of my component am I able to begin using this.$refs?

Exploring ways to integrate HTML5 audio events while initializing a Vue.js component that consists of a single file: <template> <audio ref="player" v-bind:src="activeStationUrl" v-if="activeStationUrl" controls autoplay></audio> < ...

What is the reason for node executing both of these handlers on the server?

My node app is set up with two handlers, one for requests to the root URL and another for the slug. However, when I visit the root or slug URLs, the app crashes and I receive this error: url is pointing to root url has a slug throw err; // Rethrow non-MySQ ...

Ways to conceal a column within columnSelection

Can anyone help me with setting the Column selection for Sender and Receiver in bootgrid without including Id in the column selection dropdown? I need some guidance on how to achieve this. Check out the image below for reference https://i.sstatic.net/4EEW ...

There are multiple sets of radio buttons within nested ng-repeats, but only the final group displays the selected value

I am having an issue with updating a form that contains multiple radio buttons based on data retrieved from an API. The challenge is that only the last set of radio buttons displays the value correctly. Below is the code snippet I am using (angular bracket ...

If the checkbox is selected, retrieve the product name and price and store them in an array

If the checkbox is checked, I want to retrieve the service name and its price in an array. To get the values of the selected items (i.e. service name and its price in an array), please explain how I can achieve this. $(document).on('click', &apos ...

Endless Loop Issue with jQuery AJAX Request

I've been working on a project where I'm trying to display data from a Mysqli database using jQuery ajax. However, no matter how much I tweak the code, I always end up in an infinite loop. Has anyone else encountered a similar situation when maki ...

Simply use `$timeout` inside the `$watch` method in AngularJS to create a chained

My goal is to link two $timeout functions inside a $watch. This $watch monitors user actions, and if any action is detected, both $timeout instances are canceled. Below is the code snippet outlining this scenario. .run(['$rootScope', '$loc ...

Make sure to wait for the loop to complete before moving on to the next line

I am currently leveraging the capabilities of the GitHub API to fetch a list of repositories. Subsequently, I iterate over each repository and initiate another HTTP request to obtain the most recent commit date. How can I orchestrate the iteration process ...

Jquery: Pressing Enter will cause the input field to lose

Take a look at this fiddle I created: http://jsfiddle.net/7wp9rs2s/. This is the progress I have made on my project so far. In the fiddle above, you can double click on one of the 4 items and a textbox will appear for editing. Instead of clicking out of t ...

The Bootstrap modal will not appear when the parent container's position is fixed

I am having an issue with a bootstrap modal that I am using as an instruction guide. My goal is to keep it fixed at the top-right corner of the viewport, so I tried using the fixed position. However, when I do this, the modal turns grey. On the other han ...

Is it possible to customize the MUI CSS for a specific menu item using the sx prop?

How can I apply the MenuItemStyle to the sx prop of a MenuItem using: const MenuItemStyle = { '&:hover': { backgroundColor: `${theme.color.secondaryHover}`, }, '&:selected, &:selected:hover': { backgroundColor: ...

Having trouble with the Ng multiselect dropdown displaying empty options?

I'm currently facing a challenge in adding a multiselect dropdown feature to my project. Below is the code I have been working on: HTML <ng-multiselect-dropdown [settings]="searchSettings" [data]="dummyList" multiple> </n ...

Ways to refresh UI in ReactJS without triggering a specific event

In my React application, I am displaying various pictures and GIFs that users can attach to a post. Currently, each image has an onClick handler which triggers either a modal with different options or deletes the picture if the user holds down the ctrl key ...

Exploring IdentityServer3: A Deep Dive into OAuth Flows and Varied Strategies

After developing a REST Web API application, I want to provide other developers with the capability to access those APIs on behalf of the user. To achieve this, I have implemented OAuth authentication with IdentityServer3. Currently, I have successfully ge ...

Managing large quantities of JSON-like data within MongoDB

My choice to use MongoDB is driven by the fact that Meteor doesn't officially support any other databases. The ultimate objective is to upload CSV files, parse them within Meteor, and import the data into the database. The size of the inserted data ...