0

I have objects that i parsed to build final object , so i tried to add tempObject1 and tempObject2 to OrderRequest but it is not adding to object. So i have mentioned how i want output after processing.

index.ts

export class OrderRequest {
private containingJSON = {"OrderRequest": {}};;
    public rxOrderRequest(_request: any): OrderRequest {

                const tempObject1: object = Object.assign({}, JSON.parse(strInfo));

                const tempObject2: object = Object.assign({}, JSON.parse(strNonType),
                    JSON.parse(strType)
                );
                this.containingJSON['OrderRequest'] = tempObject1;
                this.containingJSON['OrderRequest']= tempObject2


                return this;
            }
}

output

"OrderRequest": {
        User:{},
        Order: {nonCrittical:Object,critical:object}
}
2
  • Your expected object is not valid. You can either have array of objects or just an object with key/value pairs. Commented Jun 5, 2018 at 19:05
  • i have updated my question Commented Jun 5, 2018 at 19:08

1 Answer 1

2

You're re-assigning this.containingJSON['OrderRequest'] in your code, instead of creating new properties. Based on how you want, following is the updated code

export class OrderRequest {
private containingJSON = {"OrderRequest": {}};;
    public rxOrderRequest(_request: any): OrderRequest {

                const tempObject1: object = Object.assign({}, JSON.parse(strInfo));

                const tempObject2: object = Object.assign({}, JSON.parse(strNonType),
                    JSON.parse(strType)
                );
                this.containingJSON['OrderRequest']['tempObject1'] = tempObject1;// assign tempObject1 to a new property "tempObject1"
                this.containingJSON['OrderRequest']['tempObject2'] = tempObject2;


                return this;
            }
}
Sign up to request clarification or add additional context in comments.

5 Comments

its printing this { tempObject1: { User: [Object] }, tempObject2: { nonCritical: [Object], critical: [Object] } } }
but i want { User: [Object] }, Order: {nonCritical: [Object], critical: [Object] } }
In your question you mentioned that you wanted something with keys like this : "OrderRequest": { tempObject1:{}, tempObject2: {} } Now, I'm not sure if you want an object/ array of objects or anything else. This : { User: [Object] }, Order: {nonCritical: [Object], critical: [Object] } } is again not a valid single javascript object
can you show how strInfo and strNonType look like before parsing ?
i added strInfo problem is with User , Order is coming correct

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.