SmitSherlock
SmitSherlock

Reputation: 61

How to pass two array like structure in JSON fetch API

This is example code from react native doc..

fetch('https://mywebsite.com/endpoint/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'yourValue',
    secondParam: 'yourOtherValue',
  })
})

Now I want to post two array like structures or data to url and those parameters are like this below:

fetch('https://mywebsite.com/endpoint/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    0: "{"fromCity":"Mumbai, IN - Chhatrapati Shivaji International (BOM)","toCity":"Goa, IN - Dabolim (GOI)","depart":"09-04-2017","returndate":"","adult":1,"child":0,"infant":0,"classoftravel":"E","preferredairline":"AI","trip":"dom","tripType":"one way"}",
    1: "[{"selectedOnwardFlight":[{"lstExtraServices":[],"flightDuration":"01:15","departuretime":"09-04-2017T13:30:00","arrivalairport":"GOI_Dabolim, Goa","freeBaggageAllowedWeight":25,"segment":1,"departureairport":"BOM_Chhatrapati Shivaji International, Mumbai","mac":"AI_Air India","fno":"663","dpartTerInfo":"2","oac":"AI","arrivaltime":"09-04-2017T14:45:00"}],"selectedReturnFlight":[],"selectedThirdFlight":[],"selectedFourthFlight":[],"selectedFifthFlight":[],"airlineLogoPath":"AI.png","airlineName":"Air India","airlineRecommend":"N","airlinecode":"AI","faretype":"Refundable","flightsID":"0","nonStop":"Y","onduration":[["0:1:15"],["NA"],["0:1:15"],["NA"],"Chhatrapati Shivaji International, Mumbai",0],"reduration":[],"thirdduration":[],"fourthduration":[],"fifthduration":[],"fare":[{"lstExtraServices":[],"ibp":"0","cbp":"0","tp":"6528","pc":"AI","fareBasisDetails":[{"paxType":"ADULT","fareBasisCode":"TAP15B","segment":"1-663","fareType":"Refundable","rbd":"T","breakPoint":"Y"}],"fareClassType":"Economy","taxDetails":{"itaxdetails":[],"ctaxdetails":[],"atax":"496","ctax":"0","ttax":"496","itax":"0","ataxdetails":[{"name":"YR","value":"70"},{"name":"IN","value":"0"},{"name":"WO","value":"150"},{"name":"YM","value":"138"},{"name":"JN","value":"128"},{"name":"xt","value":"10"}]},"abp":"2200"}],"vendor":"amadeus","totalprice":"2713","originaltp":"2696","serviceTax":17}]",
  })
})

But this is not working ,How to post two things in POST fetch method.While attaching.Please note those 0: and 1: thing describing two seaparate json values

Upvotes: 1

Views: 1782

Answers (1)

cookie_monster
cookie_monster

Reputation: 131

The problem is that your JSON is not valid - properties such as 0 should be quoted e.g. JSON.stringify({"0": ..., then you have a quote around a start of object identifier {0: "{"fromCity" ... --> this should be {"0": {"fromCity" ...

A sample of correctly formatting the first part of your message:

{ "0": {"fromCity ": "Mumbai, IN - Chhatrapati Shivaji International(BOM)", "toCity ": "Goa, IN - Dabolim(GOI)", "depart ": "09 - 04 - 2017 ",

and the start of property 1:

"1": [{"selectedOnwardFlight ":[{"lstExtraServices ":[]

etc

A good site to use to get your JSON sorted is jsonlint.com

Upvotes: 1

Related Questions