Home > other >  I want to print "'identifier': 'OPTSTKHDFC25-08-2022PE1660.00'" in pyt
I want to print "'identifier': 'OPTSTKHDFC25-08-2022PE1660.00'" in pyt

Time:07-25

enter image description herefrom nsepython import *

import pandas as pd

import json

positions = nse_optionchain_scrapper ('HDFC')

json_decode = json.dumps(positions,indent = 4, sort_keys=True,separators =(". ", " = "))

print(json_decode['data'][0]['identifier'])

CodePudding user response:

print(json_decode['filtered']['data'][0]['PE']['identifier'])

CodePudding user response:

json_decode = json.dumps(positions,indent = 4, sort_keys=True,separators =(". ", " = "))

You can't build a JSON string (which is what json.dumps does) and then try to access part of the result as if it were the original data structure. Your json_decode is just a string, not a dict; as far as Python is concerned it has no structure beyond than the individual characters that make it up.

If you want to access parts of the data, just use positions directly:

print(positions['data'][0]['identifier'])

You can encode just that bit to JSON if you like:

print(json.dumps(positions['data'][0]['identifier'])

but that's probably just a quoted string in this case.

So I'm not sure what your goal is. If you want to print out the JSON version of positions, great, just print it out. But the JSON form is for input and output only; it's not suitable for messing around with inside your Python code.

  • Related