Jack N
Jack N

Reputation: 175

Reformatting JSON by Placing Object into Array

I'm trying to reformat my JSON to work with an external API

My current JSON Structure:

{
    "serviceConfigs": {
        "servicecode": "SC1",
        "specifiers": {
            "Brand ID": {
                "text": {
                    "value": "test",
                    "type": "text"
                }
            },
            "Program ID": {
                "text": {
                    "value": "test",
                    "type": "text"
                }
            }
        }
    }
}

Desired Output:

{
    "serviceConfigs": [{
        "servicecode": "SC1",
        "specifiers": {
            "Brand ID": {
                "text": {
                    "value": "test",
                    "type": "text"
                }
            },
            "Program ID": {
                "text": {
                    "value": "test",
                    "type": "text"
                }
            }
        }
    }]
}

So currently serviceConfigs is in an Object, but I want it into an array

My current thought process is to use a push command, but I'm not sure how to access the object (Loop?).

Upvotes: 0

Views: 42

Answers (1)

Code Maniac
Code Maniac

Reputation: 37755

You can access the key's value and place it back to same key in desired format.

let obj = {"serviceConfigs": {"servicecode": "SC1","specifiers": {"Brand ID": {"text": {"value": "test","type": "text"}},"Program ID": {"text": {"value": "test","type": "text"}}}}}

obj.serviceConfigs = [obj.serviceConfigs]

console.log(obj)

Upvotes: 1

Related Questions