python 3.x – How to declare pydantic BaseModel in FastAPI to receive a valid json object

A client app is sending data to server using POST method. Data basically has 4 key-value pairs out of which value of the secod key is a valid json object with multiple key-value pairs in it. It currently looks like this

{
  "name": "string",
  "content": {{"resolution":"1","symbol_type":"stock","exchange":"NSE","listed_exchange":"","symbol":"NSE:SBIN-EQ","short_name":"SBIN","legs":"[{"symbol":"SBIN","pro_symbol":"SBIN"}]","name":"asdwa","description":"","charts_symbols":"{"1":{"symbol":"NSE:SBIN-EQ"}}","is_realtime":"1","content":"{"name":"asdwa","layout":"s","charts":[{"panes":[{"sources":[{"type":"MainSeries","id":"RGTnHz","state":{"style":1,"esdShowDividends":true,"esdShowSplits":true,"esdShowEarnings":true,"esdShowBreaks":false,"esdFlagSize":2,"showCountdown":false,"bidAsk":{"visible":false,"lineStyle":1,"lineWidth":1,"bidLineColor":"#2962FF","askLineColor":"#EF5350"},"prePostMarket":{"visible":true,"lineStyle":1,"lineWidth":1,"preMarketColor":"#fb8c00","postMarketColor":"#2962FF"},"highLowAvgPrice":{"highLowPriceLinesVisible":false,"highLowPriceLabelsVisible":false,"averageClosePriceLineVisible":false,"averageClosePriceLabelVisible":false},"showInDataWindow":true,"visible":true,"showPriceLine":true,"priceLineWidth":1,"priceLineColor":"","baseLineColor":"#B2B5BE","showPrevClosePriceLine":false,"prevClosePriceLineWidth":1,"prevClosePriceLineColor":"rgba( 85, 85, 85, 1)","minTick":"default","dividendsAdjustment":{},"sessionId":"regular","sessVis":false,"statusViewStyle":{"fontSize":16,"showExchange":true,"showInterval":true,"symbolTextSource":"description"},"candleStyle":{"upColor":"#26a69a","downColor":"#ef5350","drawWick":true,"drawBorder":true,"borderColor":"#378658","borderUpColor":"#26a69a","borderDownColor":"#ef5350","wickColor":"#737375","wickUpColor":"#26a69a","wickDownColor":"#ef5350","barColorsOnPrevClose":false,"drawBody":true},"hollowCandleStyle":{"upColor":"#26a69a","downColor":"#ef5350","drawWick":true,"drawBorder":true,"borderColor":"rgba( 55, 134, 88, 1)","borderUpColor":"#26a69a","borderDownColor":"#ef5350","wickColor":"rgba( 115, 115, 117, 1)","wickUpColor":"#26a69a","wickDownColor":"#ef5350","drawBody":true},"haStyle":{"upColor":"#26a69a","downColor":"#ef5350","drawWick":true,"drawBorder":true,"borderColor":"rgba( 55, 134, 88, 1)","borderUpColor":"#26a69a","borderDownColor":"#ef5350","wickColor":"rgba( 115, 115, 117, 1)","wickUpColor":"#26a69a","wickDownColor":"#ef5350","showRealLastPrice":false,"barColorsOnPrevClose":false,"inputs":{},"inputInfo":{},"drawBody":true},"barStyle":{"upColor":"#26a69a","downColor":"#ef5350","barColorsOnPrevClose":false,"dontDrawOpen":false,"thinBars":true},"hiloStyle":{"color":"#2962FF","showBorders":true,"borderColor":"#2962FF","showLabels":true,"labelColor":"#2962FF","fontSize":7,"drawBody":true},"lineStyle":{"color":"#2962FF","linestyle":0,"linewidth":2,"priceSource":"close","styleType":2},"areaStyle":{"color1":"rgba(41, 98, 255, 0.28)","color2":"#2962FF","linecolor":"#2962FF","linestyle":0,"linewidth":2,"priceSource":"close","transparency":100},"renkoStyle":{"upColor":"#26a69a","downColor":"#ef5350","borderUpColor":"#26a69a","borderDownColor":"#ef5350","upColorProjection":"rgba( 169, 220, 195, 1)","downColorProjection":"rgba( 245, 166, 174, 1)","borderUpColorProjection":"rgba( 169, 220, 195, 1)","borderDownColorProjection":"rgba( 245, 166, 174, 1)","wickUpColor":"#26a69a","wickDownColor":"#ef5350","inputs":{"source":"close","sources":"Close","boxSize":3,"style":"ATR","atrLength":14,"wicks":true},"inputInfo":{"source":{"name":"source"},"sources":{"name":"Source"},"boxSize":{"name":"Box size"},"style":{"name":"Style"},"atrLength":{"name":"ATR length"},"wicks":{"name":"Wicks"}}},"pbStyle":{"upColor":"#26a69a","downColor":"#ef5350","borderUpColor":"#26a69a","borderDownColor":"#ef5350","upColorProjection":"rgba( 169, 220, 195, 1)","downColorProjection":"rgba( 245, 166, 174, 1)","borderUpColorProjection":"rgba( 169, 220, 195, 1)","borderDownColorProjection":"rgba( 245, 166, 174, 1)","inputs":{"source":"close","lb":3},"inputInfo":{"source":{"name":"Source"},"lb":{"name":"Number of line"}}},"kagiStyle":{"upColor":"#26a69a","downColor":"#ef5350","upColorProjection":"rgba( 169, 220, 195, 1)","downColorProjection":"rgba( 245, 166, 174, 1)","inputs":{"source":"close","style":"ATR","atrLength":14,"reversalAmount":1},"inputInfo":{"source":{"name":"Source"},"style":{"name":"Style"},"atrLength":{"name":"ATR length"},"reversalAmount":{"name":"Reversal amount"}}},"pnfStyle":{"upColor":"#26a69a","downColor":"#ef5350","upColorProjection":"rgba( 169, 220, 195, 1)","downColorProjection":"rgba( 245, 166, 174, 1)","inputs":{"sources":"Close","reversalAmount":3,"boxSize":1,"style":"ATR","atrLength":14,"oneStepBackBuilding":false},"inputInfo":{"sources":{"name":"Source"},"boxSize":{"name":"Box size"},"reversalAmount":{"name":"Reversal amount"},"style":{"name":"Style"},"atrLength":{"name":"ATR length"},"oneStepBackBuilding":{"name":"One step back building"}}},"baselineStyle":{"baselineColor":"rgba( 117, 134, 150, 1)","topFillColor1":"rgba( 38, 166, 154, 0.28)","topFillColor2":"rgba( 38, 166, 154, 0.05)","bottomFillColor1":"rgba( 239, 83, 80, 0.05)","bottomFillColor2":"rgba( 239, 83, 80, 0.28)","topLineColor":"rgba( 38, 166, 154, 1)","bottomLineColor":"rgba( 239, 83, 80, 1)","topLineWidth":2,"bottomLineWidth":2,"priceSource":"close","transparency":50,"baseLevelPercentage":50},"rangeStyle":{"upColor":"#26a69a","downColor":"#ef5350","thinBars":true,"upColorProjection":"rgba( 169, 220, 195, 1)","downColorProjection":"rgba( 245, 166, 174, 1)","inputs":{"range":10,"phantomBars":false},"inputInfo":{"range":{"name":"Range"},"phantomBars":{"name":"Phantom bars"}}},"symbol":"NSE:SBIN-EQ","shortName":"SBIN","timeframe":"","onWidget":false,"interval":"1","unitId":null,"currencyId":null,"priceAxisProperties":{"autoScale":true,"autoScaleDisabled":false,"lockScale":false,"percentage":false,"percentageDisabled":false,"log":false,"logDisabled":false,"alignLabels":true,"isInverted":false,"indexedTo100":false}},"zorder":0,"haStyle":{"studyId":"BarSetHeikenAshi@tv-basicstudies-60"},"renkoStyle":{"studyId":"BarSetRenko@tv-prostudies-40"},"pbStyle":{"studyId":"BarSetPriceBreak@tv-prostudies-34"},"kagiStyle":{"studyId":"BarSetKagi@tv-prostudies-34"},"pnfStyle":{"studyId":"BarSetPnF@tv-prostudies-34"},"rangeStyle":{"studyId":"BarSetRange@tv-basicstudies-72"}},{"type":"study_Volume","id":"bl7jkV","state":{"styles":{"vol":{"linestyle":0,"linewidth":1,"plottype":5,"trackPrice":false,"transparency":50,"visible":true,"color":"#000080","histogramBase":0,"joinPoints":false,"title":"Volume"},"vol_ma":{"linestyle":0,"linewidth":1,"plottype":0,"trackPrice":false,"transparency":0,"visible":false,"color":"#2196f3","histogramBase":0,"joinPoints":false,"title":"Volume MA"}},"palettes":{"volumePalette":{"colors":{"0":{"color":"#EF5350","width":1,"style":0,"name":"Falling"},"1":{"color":"#26A69A","width":1,"style":0,"name":"Growing"}}}},"inputs":{"showMA":false,"length":20,"col_prev_close":false},"precision":"default","bands":{},"area":{},"graphics":{},"showInDataWindow":true,"visible":true,"showStudyArguments":true,"intervalsVisibilities":{"ticks":true,"seconds":true,"secondsFrom":1,"secondsTo":59,"minutes":true,"minutesFrom":1,"minutesTo":59,"hours":true,"hoursFrom":1,"hoursTo":24,"days":true,"daysFrom":1,"daysTo":366,"weeks":true,"weeksFrom":1,"weeksTo":52,"months":true,"monthsFrom":1,"monthsTo":12,"ranges":true},"plots":{"0":{"id":"vol","type":"line"},"1":{"id":"volumePalette","palette":"volumePalette","target":"vol","type":"colorer"},"2":{"id":"vol_ma","type":"line"}},"_metainfoVersion":52,"isTVScript":false,"isTVScriptStub":false,"is_hidden_study":false,"description":"Volume","shortDescription":"Volume","is_price_study":false,"id":"Volume@tv-basicstudies","format":{"type":"volume"},"description_localized":"Volume","shortId":"Volume","packageId":"tv-basicstudies","version":"1","fullId":"Volume@tv-basicstudies-1","productId":"tv-basicstudies","name":"Volume@tv-basicstudies","_serverMetaInfoVersion":52},"zorder":-10000,"ownFirstValue":null,"metaInfo":{"palettes":{"volumePalette":{"colors":{"0":{"name":"Falling"},"1":{"name":"Growing"}}}},"inputs":[{"id":"showMA","name":"show MA","defval":false,"type":"bool","isHidden":true},{"id":"length","name":"MA Length","defval":20,"type":"integer","min":1,"max":2000},{"defval":false,"id":"col_prev_close","name":"Color based on previous close","type":"bool"}],"plots":[{"id":"vol","type":"line"},{"id":"volumePalette","palette":"volumePalette","target":"vol","type":"colorer"},{"id":"vol_ma","type":"line"}],"graphics":{},"defaults":{"styles":{"vol":{"linestyle":0,"linewidth":1,"plottype":5,"trackPrice":false,"transparency":50,"visible":true,"color":"#000080"},"vol_ma":{"linestyle":0,"linewidth":1,"plottype":0,"trackPrice":false,"transparency":0,"visible":false,"color":"#2196F3"}},"palettes":{"volumePalette":{"colors":{"0":{"color":"#EF5350","width":1,"style":0},"1":{"color":"#26A69A","width":1,"style":0}}}},"inputs":{"showMA":false,"length":20,"col_prev_close":false}},"_metainfoVersion":52,"isTVScript":false,"isTVScriptStub":false,"is_hidden_study":false,"styles":{"vol":{"title":"Volume","histogramBase":0},"vol_ma":{"title":"Volume MA","histogramBase":0}},"description":"Volume","shortDescription":"Volume","is_price_study":false,"id":"Volume@tv-basicstudies-1","format":{"type":"volume"},"description_localized":"Volume","shortId":"Volume","packageId":"tv-basicstudies","version":"1","fullId":"Volume@tv-basicstudies-1","productId":"tv-basicstudies","name":"Volume@tv-basicstudies","_serverMetaInfoVersion":52}}],"leftAxisesState":[],"rightAxisesState":[{"state":{"id":"AEh8VAL7anFf","m_priceRange":{"m_maxValue":505.6,"m_minValue":494},"m_isAutoScale":true,"m_isPercentage":false,"m_isIndexedTo100":false,"m_isLog":false,"m_isLockScale":false,"m_isInverted":false,"m_height":641.6,"m_topMargin":0.1,"m_bottomMargin":0.08,"alignLabels":true},"sources":["RGTnHz"]}],"overlayPriceScales":{"bl7jkV":{"id":"aYA6FWoeiTkP","m_priceRange":{"m_maxValue":298353,"m_minValue":0},"m_isAutoScale":true,"m_isPercentage":false,"m_isIndexedTo100":false,"m_isLog":false,"m_isLockScale":false,"m_isInverted":false,"m_height":641.6,"m_topMargin":0.1,"m_bottomMargin":0.08,"alignLabels":true}},"stretchFactor":2000,"mainSourceId":"RGTnHz","priceScaleRatio":null}],"timeScale":{"m_barSpacing":6,"m_rightOffset":10},"chartProperties":{"paneProperties":{"backgroundType":"solid","background":"#ffffff","backgroundGradientStartColor":"#ffffff","backgroundGradientEndColor":"#ffffff","vertGridProperties":{"color":"rgba(42, 46, 57, 0.06)","style":0},"horzGridProperties":{"color":"rgba(42, 46, 57, 0.06)","style":0},"crossHairProperties":{"color":"#9598A1","style":2,"transparency":0,"width":1},"topMargin":10,"bottomMargin":8,"axisProperties":{"autoScale":true,"autoScaleDisabled":false,"lockScale":false,"percentage":false,"percentageDisabled":false,"indexedTo100":false,"log":false,"logDisabled":false,"alignLabels":true,"isInverted":false},"legendProperties":{"showStudyArguments":true,"showStudyTitles":true,"showStudyValues":true,"showSeriesTitle":true,"showSeriesOHLC":true,"showLegend":true,"showBarChange":true,"showBackground":true,"backgroundTransparency":50,"wrapText":false}},"scalesProperties":{"backgroundColor":"#ffffff","lineColor":"rgba(42, 46, 57, 0.14)","textColor":"#131722","fontSize":12,"scaleSeriesOnly":false,"showSeriesLastValue":true,"seriesLastValueMode":1,"showSeriesPrevCloseValue":false,"showStudyLastValue":false,"showSymbolLabels":false,"showStudyPlotLabels":false,"showBidAskLabels":false,"showPrePostMarketPriceLabel":true,"showFundamentalNameLabel":false,"showFundamentalLastValue":false,"barSpacing":6,"showCurrency":true,"showUnit":true},"chartEventsSourceProperties":{"visible":true,"futureOnly":true,"breaks":{"color":"rgba(85, 85, 85, 1)","visible":false,"style":2,"width":1}},"tradingProperties":{"showPositions":true,"positionPL":{"visibility":true,"display":0},"showOrders":true,"showExecutions":true,"horizontalAlignment":0,"extendLeft":true,"lineLength":5,"lineWidth":1,"lineStyle":0},"priceScaleSelectionStrategyName":"auto"},"lineToolsGroups":{"groups":[]},"version":3,"timezone":"Asia/Kolkata","sessions":{"properties":{"graphics":{"backgrounds":{"outOfSession":{"color":"#2962FF","transparency":92,"visible":false},"preMarket":{"color":"#FF9800","transparency":92,"visible":false},"postMarket":{"color":"#2962FF","transparency":92,"visible":false}},"vertlines":{"sessBreaks":{"color":"#4985e7","style":2,"visible":false,"width":1}}}}},"shouldBeSavedEvenIfHidden":false,"chartId":"1"}]}"}},
  "symbol": "string",
  "resolution": "string"
}

I tried following BaseModel declaration and async fuction

class Item(BaseModel):
    name: str
    content: dict
    symbol: str
    resolution: str

@app.post("/saveload/v1/charts")
async def create_item(item: Item, q: str | None = None):
    return item

but this leads to error 422 Unprocessable Entity as you can see the dict wraps json in its curly braces due to which FastAPI is throwing this error. So how should I declare BaseModel to receive data. Thank you in advance.

Leave a Comment