-3

I have to send this JSON format to api as post request. But when checked on line on JSONLint it says it is wrong, error. Please guide to create correct. Below is the JSON format:

{
"departure_time" = "2017-07-28T17:39:43.611Z";
steps = ({
    "encoded_polyline" = "ytfzDqzksM_AgB";
    "road_name" = "ytfzDqzksM_AgB";
}, {
    "encoded_polyline" = "yvfzDy}ksMPMf@a@";
    "road_name" = "yvfzDy}ksMPMf@a@";
}, {
    "encoded_polyline" = "_ufzDi_lsM}@mB";
    "road_name" = "_ufzDi_lsM}@mB";
}, {
    "encoded_polyline" = "}vfzDwblsMb@a@f@a@";
    "road_name" = "}vfzDwblsMb@a@f@a@";
}, {
    "encoded_polyline" = "qtfzD{dlsMoA}B";
    "road_name" = "qtfzD{dlsMoA}B";
});
vehicle = {
    axles = 2;
};
}

Please guide what is wrong in it and how to correct it.

iPhone 7
  • 1,731
  • 1
  • 27
  • 63
  • 1
    The `;` should be `,`, the `(` and `)` have to be `[` and `]`. – luk2302 Aug 24 '18 at 10:49
  • but in system this can not be generated i guess – iPhone 7 Aug 24 '18 at 10:50
  • Neither `=` nor `;` belong in JSON. JSON objects are of form `{ "key": value, "key": value }`, not `{ "key" = value; key = value }`. Arrays are `[val1, val2]`, not `(val1; val2;)`. – Amadan Aug 24 '18 at 10:50
  • but how can i generate this i created dictionary and set objects in them – iPhone 7 Aug 24 '18 at 10:52
  • 1
    How are you generating this ... thingy that is not json but is supposed to? – luk2302 Aug 24 '18 at 10:52
  • Please check this "https://stackoverflow.com/questions/52000079/how-to-hit-post-request-with-nsdictionary-as-parameter/52000640?noredirect=1#comment90951169_52000640" – iPhone 7 Aug 24 '18 at 10:52
  • well you may want to use code that has actually anything to do with json, google for it, tons of resources out there. – luk2302 Aug 24 '18 at 10:55
  • You are just saying "printing @_attributes", not how you're doing it. If you're not using `NSJSONSerialization`, you're likely doing it wrong. – Amadan Aug 24 '18 at 10:55
  • You need NSString declared with data and encoding .utf8 – Shehata Gamal Aug 24 '18 at 11:03
  • I'd use https://jsonformatter.curiousconcept.com/. – GiamPy Aug 24 '18 at 11:07
  • @Amadan NSJSONSerialization used for parsing not for what i required(send dictionary in json format) – iPhone 7 Aug 24 '18 at 11:13
  • You are wrong. It is used for both. See [Generate JSON string from NSDictionary in iOS](https://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary-in-ios) for example of JSON generation. – Amadan Aug 24 '18 at 11:16

2 Answers2

-1

First of all, semicolons must be replaced by comas to separate lines, EXCEPT the last line that doesn't need anything:

"encoded_polyline" = "qtfzD{dlsMoA}B",
"road_name" = "qtfzD{dlsMoA}B"

To declare an array, use brackets, not parentheses:

"steps" = [{
    "encoded_polyline" = "ytfzDqzksM_AgB";
    "road_name" = "ytfzDqzksM_AgB";
}, {
    "encoded_polyline" = "yvfzDy}ksMPMf@a@";
    "road_name = "yvfzDy}ksMPMf@a@";
}]

And finally, equals symbols aren't used in JSON. Instead, use colons:

"departure_time" : "2017-07-28T17:39:43.611Z"
-1

Validated JSON

{
    "departure_time": "2017-07-28T17:39:43.611Z",
    "steps": [{
        "encoded_polyline": "ytfzDqzksM_AgB",
        "road_name": "ytfzDqzksM_AgB"
    }, {
        "encoded_polyline": "yvfzDy}ksMPMf@a@",
        "road_name": "yvfzDy}ksMPMf@a@"
    }, {
        "encoded_polyline": "_ufzDi_lsM}@mB",
        "road_name": "_ufzDi_lsM}@mB"
    }, {
        "encoded_polyline": "}vfzDwblsMb@a@f@a@",
        "road_name": "}vfzDwblsMb@a@f@a@"
    }, {
        "encoded_polyline": "qtfzD{dlsMoA}B",
        "road_name": "qtfzD{dlsMoA}B"
    }],
    "vehicle": {
        "axles": 2
    }
}

Verified on - https://jsonlint.com/

Abhishek Jadhav
  • 706
  • 5
  • 12