Json object is aligned

I am using node.js with an express parser. My json input object is in the following format

{
    ObjectA:
    {       
        objectB : [
                    actions : [
                        {                           

                            conditions : 
                                paramA: [
                                {
                                    paramB: 45,
                                    paramC: "00:01"
                                }

      

it is complex and very nested. When I get an object in req.body I get it in the following format

 ObjectA.ObjectB.actions.[conditions][paramA][0][paramB]

      

Until it reaches the conditions, the entire structure decays and puts the entire data structure into one object. Is there some kind of limitation for nested objects using expression or node.

I know this is not a problem with the code, because if I decrease the nesting everything will be fine. Reducing nesting means moving conditions directly to ObjectA.

I am using the following to set up the server

   app.use(logger('combined',{stream: expressLogFile}));
   var customValidator = new CustomValidator();
  app.use(bodyParser());
  app.use(expressValidator({
        customValidators: customValidator.validations
  }));

  app.use(methodOverride());

      

Express version: "express": "4.9.5"

This happens when I receive data from req.body on the server side. The req.body data looks different than what I passed from the client HTTP POST request.

OK ... the exact structure looks like this

{

        content: {
            f: {
                z: 'b2a98061-9b3b-4ff6-8164-164892d369ad',
                actions: {
                    myactions: [
                        {
                            name : 'My message actions1',
                            title : 'This is the title of my message.1',
                            message : 'Have a nice day.1',
                            conditions: {
                                condition1:
                                    [
                                        {
                                            percentage: 45,
                                            timeoutPeriod: "00:01"
                                        }
                                    ],
                                condition2: [
                                    {
                                        start: "12/12/2014",
                                        end: "14/12/2014"
                                    }
                                ],
                                condition3: [{
                                    from: {
                                        time: "10:00",
                                        period: "am"
                                    },
                                    to: {
                                        time: "2:00",
                                        period: "pm"
                                    }
                                }]
                            }
                        }
                    ]
                }
            }
        }
    }

      

I am expecting data from req.body in the format content.f.action.myactions [0] .conditions.condition1 [0] .precentage but I am revising content.f.action.myactions [0]. "[conditions] [condition1] [0] [PRECENTAGE]"

+3


source to share


1 answer


I faced the same problem and I also suspect the body-parser module on the nodejs node side. In my case, the problem was on the JSON generation side. I used to send my arguments (direct javascript objects) as data without specifying content type. I changed the original value:

registerHandlers($.ajax({
    'url': "/api/" + name,
    'type': 'POST',
    'data': args
}), name, onSuccess, onFailure, onError);

      

in the following way:



registerHandlers($.ajax({
    'url': "/api/" + name,
    'type': 'POST',
    'data': JSON.stringify(args),
    'contentType': 'application/json'
}), name, onSuccess, onFailure, onError);

      

and then the JSON was recovered just fine on the other end.

+1


source







All Articles