D
D
Dmitry2020-12-14 22:31:38
JavaScript
Dmitry, 2020-12-14 22:31:38

Convert JSON object to string and output?

Hello. Please tell me what to do!
There is a task. "Pull out" information from the JSON file and enter the necessary data in the popup, but one of the properties of the object contains a bunch of other objects (In general, this should be a code example)
Example:
This is the data of the object and its properties

"registration":{
            "link":"/api/auth/registration",
            "api":{
                "result": true,
                "data": {
                    "name": "test",
                    "email": "[email protected]",
                    "api_token": "dYb4HQMwIZ0YXtkEBNQnsuekupAwCuMykWUFO6xB0kgHYqvgylGIFzLT8WO9",
                    "id": 34
                },
                "code": 0
            }
        },

That is, "registration" is an object, link and api are its properties.
link is of course passed as a string. And I can pass it to HTML as text without any problems, and to api as objects with properties (I just tried to quote everything in api, but I get an error in the syntax)

The information output form is as follows:
const xhr = new XMLHttpRequest;
                xhr.responseType = 'json';
                xhr.addEventListener('load', function(){
                window.api_schema = xhr.response;

                for(let btn of btns){
                    btn.addEventListener('click', () => {
                    let content = btn.dataset.api;
                
                    modalLink.innerHTML = window.api_schema[content].link;
                    JSON.stringify(window.api_schema[content].api) 
                    textApiCode.innerHTML = window.api_schema[content].api;
                    
                    ModelPrimer.classList.remove('api-modal-none');
            })

And in the end, what the link stores is transmitted and displayed without problems, but what is in the api is [object Object]
Help what to do?
PS As seen in the code. I tried to convert the received data to a string, but it did not work. In addition, as far as I understand, it removes spaces and line breaks, but I would like to leave everything in the same order, since all api information will be transferred to the tag and retain its structure.
Thanks a lot!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-12-14
@Azazel1928

It?JSON.stringify(api, null, 2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question