Why is my JSON parse function returning an empty string?

Not sure if the issue lies with VueJS or JS itself.

Within my database, I have a string (converted from a JS Object using JSON.stringify()) that appears as follows:

{"type":5,"values":{"7":"/data/images/structured-content/64-7-scico.jpg","8":"<b>wefwe</b>","9":"Nějaký text","10":"/data/images/structured-content/64-10-scico.jpg"}}

The goal is to retrieve this string from the database (using Axios), convert it back into a JS object, and assign it to the data in VueJS:

.then(response => {

            if (response.data.response === "ok" && response.status == 200) {

                // get data
                let data = response.data.data.data[0];

                // pass name to state
                this.objectName = data.name;

                // get json in string format
                let result = data.content;

                // first log
                console.log(result);

                // convert string to json
                let content = JSON.parse( result );

                // second log
                console.log( content.values );

                // update states
                this.IDType = content.type;
                this.values = content.values;


            }

        })

Axios, data.name, and content.type are functioning properly. However, the second log (content.values) seems to be returning an observer with empty strings, preventing me from passing them to Vue data effectively. The values within this object remain consistently empty, as depicted below on the screen image. What could be causing this issue? Appreciate your insights!

https://i.sstatic.net/kayWV.png

Debugger output:

https://i.sstatic.net/tXi5s.png

Answer №1

In order for Vue to be reactive, it requires prior knowledge of the keys within an object before they are assigned. If the keys are already known, it is advisable to pre-assign them like this:

data : () => ({
  values : {
     7 : "",
     8 : "",
     9 : "",
     10 : ""
  }
}),

You can then utilize Object.assign to set these values:

Object.assign(this.values, content.values)

For cases where the keys are unknown or dynamic, you can make them reactive by using the $set method:

Object.keys(content.values).forEach(
  function(key){
     this.$set(this.value, key, content.values[key])
  }
)

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 there a way to verify the authenticity of a survey depending on the types of form elements used

Creating a form in PHP with various dynamic form elements like radio buttons, text fields, and dropdowns. Seeking to validate this form using JQuery based on the question type, which is identified by the names q1, q2, etc. $(function(){ if ($(&apo ...

When using multer to upload a file, the issue of req.files being undefined may

I am currently working on a Node.js Application using Express.js 4 that involves uploading an image. I opted to utilize the multer module for this task, but encountered an issue with accessing the uploaded file via req.files. Below is the relevant portions ...

Converting an array of object keys into integers only can be done using JavaScript

Here is an array of objects with various nutrients and their values: [ {nutrient: "Energy", per100: "449kcal", per35: "157kcal", id: 6} {nutrient: "Fat", per100: "24.4g", per35: "8.6g", id: 1} {nutrient: "Saturated fat", per100: "4.5g", per35: "1.6g ...

Despite setting allowHalfOpen to True, Node.js: Client still disconnects from Server

I've been working on a Node.js script that involves creating a server if a specific port is available, or connecting to an existing server if the port is already in use. To achieve this, I am using a recursive approach based on a reference from this l ...

Troubleshooting issue with decoding encoded JSON in PHP

Below is the content of my data.php file: $global = $pdo->query("SELECT name, age"); $global = $global->fetchAll(PDO::FETCH_ASSOC); $arr = array('data'=>$global); echo json_encode($arr, JSON_PRETTY_PRINT); This is what my index.php ...

Export nested objects from an AngularJS JSON array to a CSV file

When I download my data into a CSV file, the display setting is showing as "[object Object]". This is not the desired outcome. https://i.stack.imgur.com/ej2UO.png The expected display should look like this: https://i.stack.imgur.com/8JJ88.png This is p ...

Tips on using the .map() method to extract data from a JSON response received from a get request and utilizing the content within a specific index to populate table rows

Here is the JSON response representation, https://i.stack.imgur.com/0QWkv.png This is how my project displays it: https://i.stack.imgur.com/LnA5v.png The rendering code snippet is as follows: render() { const { materials } = this.state; ...

Tips for preventing redundant function calls to a prop within a ReactJS component

I am currently facing an issue where I am repeatedly calling the same function within a component to retrieve a specific prop inside the render function. Is there a way to optimize this and avoid calling the function multiple times for each required prop ...

Authenticate users using Azure Active Directory in a Vue.js application

Exploring Vue.js and Azure for the first time. Tasked with developing a Login interface in Vue that authenticates users based on their roles in Azure. Curious about the process of connecting Vue and Azure AD. Any suggestions or ideas on how to achieve thi ...

Developing HTML5 animation by utilizing sprite sheets

Struggling to create an engaging canvas animation with the image provided in the link below. Please take a look at https://i.stack.imgur.com/Pv2sI.jpg I'm attempting to run this sprite sheet image for a normal animation, but unfortunately, I seem to ...

A guide to creating a JavaScript function that outputs a script in string form

Currently, I am utilizing angular and seeking to add a script to my directive's template. My goal is to create a function that can take the necessary parameters for the script and return it as a string. This approach would prevent me from having to em ...

A lateral sliding motion

Here is the HTML code I am working with: <div class="menuTabs"> <div class="mtabArrowLeft">Left</div> <input class="menutabBTN" name="" type="button" value="a" /> <input class="menutabBTN" name="" type="b ...

Java - Converting JSON format

As a beginner in Java, I am exploring ways to transform Json data just like in the following example. I'm unsure whether to use json array, mapper or List for this task Here's the scenario: Input Document: { "data1": "A", ...

Display a preview window once the image has been chosen

I have created an image preview div to show the selected image's thumbnail. Everything was working fine so far. But now, I want this div to be hidden when the page initially loads and only become visible when a user selects an image to upload. Here is ...

Utilizing PNG images with color in CSS, HTML, or JavaScript

On my website, I have an image in PNG format. I am wondering if there is a way to change the color of the image using HTML5, JavaScript, or CSS. Ideally, I would like the image to be changed to white by inverting its colors (changing black to white, not al ...

Ways to conceal the 'Return to Top' button in a script that is only revealed after navigating to the bottom of the page

Can anyone help me hide the 'Back to Top' button in a script that only appears after scrolling to the bottom of the page? I need to take screenshots without it showing up. I've tried using the code below, but the 'Back to Top' but ...

Issue in Vue/Node application: Rule is restricted to a single resource source

I just recently started diving into Vue and Node, and things were going smoothly with a Vue3 project I was experimenting with to enhance my skills. I decided to incorporate scss files, so I went ahead and installed sass-loader via npm using: npm install sa ...

Juicer- Setting restrictions on the frequency of posts

How can I limit the number of posts displayed using the react-juicer-feed component? import { Feed } from 'react-juicer-feed'; const MyFeed = () => { return ( <Feed feedId="<feed-id>" perPage={3} /> ...

Integrate Vue Ant Design to showcase validations on both the client and server sides within the Ant Form using v-decorator

Utilizing the form component from Ant design along with v-decorators for validating forms has been successful in implementing client-side validation. However, I am now looking to incorporate server-side form data validations into my current setup. Let&apo ...

Issues with the initial calculator project I built using JavaScript (excluding HTML and CSS)

My first calculator is nearly complete, but I have encountered a challenge. The functionality of my calculator is quite simple; it prompts the user for input using window.prompt and performs addition, subtraction, multiplication, or division based on the u ...