0

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?).

1
  • 3
    data.serviceConfigs = [data.serviceConfigs]? Commented Mar 20, 2019 at 16:04

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.