I wanna convert below JSON data to string data same as below.
{
"Time Series (Daily)": {
"2020-04-02": {
"1. open": "1693.5300",
"2. high": "1726.7600",
"3. low": "1664.1300",
"4. close": "1724.8600",
"5. volume": "766313"
},
"2020-04-01": {
"1. open": "1737.2800",
"2. high": "1762.4399",
"3. low": "1685.3700",
"4. close": "1685.4600",
"5. volume": "1243600"
}
}
convert to like this
^KS11 |20200402|1664.1300 |1693.5300 |1726.7600 |1724.8600 |
^KS11 |20200401|1685.4600 |1737.2800 |1762.4399 |1685.3700 |
If you were provided the json data as a string, I would suggest using the json package, like so:
import json
jdata = """
{
"Time Series (Daily)": {
"2020-04-02": {
"1. open": "1693.5300",
"2. high": "1726.7600",
"3. low": "1664.1300",
"4. close": "1724.8600",
"5. volume": "766313"
},
"2020-04-01": {
"1. open": "1737.2800",
"2. high": "1762.4399",
"3. low": "1685.3700",
"4. close": "1685.4600",
"5. volume": "1243600"
}
}
}"""
def day_to_str(day, data):
return '|'.join(('^KS11 ',
day.replace('-', ''),
data['1. open'],
data['2. high'],
data['3. low'],
data['4. close'],
''))
time_series = json.loads(jdata)['Time Series (Daily)']
for day in time_series:
print(day_to_str(day, time_series[day]))
Output:
^KS11 |20200402|1693.5300|1726.7600|1664.1300|1724.8600|
^KS11 |20200401|1737.2800|1762.4399|1685.3700|1685.4600|
If you didn't want to get the volume
,and it always in the last.You can try this:
yourDict = { # assume it is a dict.Json is also OK.
"Time Series (Daily)": {
"2020-04-02": {
"1. open": "1693.5300",
"2. high": "1726.7600",
"3. low": "1664.1300",
"4. close": "1724.8600",
"5. volume": "766313"
},
"2020-04-01": {
"1. open": "1737.2800",
"2. high": "1762.4399",
"3. low": "1685.3700",
"4. close": "1685.4600",
"5. volume": "1243600"
}
}
}
temp = "^KS11 |{} |\n"
result = ""
temp = "^KS11 |{} |\n" # put data into {}
result = "" # use string
# Easy string handle
for i in yourDict["Time Series (Daily)"]:
# get the value in each dict
numberList = list(yourDict["Time Series (Daily)"][i].values())[:-1] # extract them
oneLineNumber = " |".join(numberList)# make them together
oneLineStr = temp.format(oneLineNumber) # Add them
result += oneLineStr
print(result)
The result(more one "\\n"):
^KS11 |20200402|1693.5300 |1726.7600 |1664.1300 |1724.8600 |
^KS11 |20200401|1737.2800 |1762.4399 |1685.3700 |1685.4600 |
Or one line code:
yourDict = {
"Time Series (Daily)": {
"2020-04-02": {
"1. open": "1693.5300",
"2. high": "1726.7600",
"3. low": "1664.1300",
"4. close": "1724.8600",
"5. volume": "766313"
},
"2020-04-01": {
"1. open": "1737.2800",
"2. high": "1762.4399",
"3. low": "1685.3700",
"4. close": "1685.4600",
"5. volume": "1243600"
}
}
}
print("\n".join(["^KS11 |{} |".format(i.replace("-","")+"|"+" |".join(list(yourDict["Time Series (Daily)"][i].values())[:-1])) for i in yourDict["Time Series (Daily)"]]))
This is(no more "\\n"):
^KS11 |20200402|1693.5300 |1726.7600 |1664.1300 |1724.8600 |
^KS11 |20200401|1737.2800 |1762.4399 |1685.3700 |1685.4600 |
(Maybe there is better way)
import json
data = '''
{
"Time Series (Daily)": {
"2020-04-02": {
"1. open": "1693.5300",
"2. high": "1726.7600",
"3. low": "1664.1300",
"4. close": "1724.8600",
"5. volume": "766313"
},
"2020-04-01": {
"1. open": "1737.2800",
"2. high": "1762.4399",
"3. low": "1685.3700",
"4. close": "1685.4600",
"5. volume": "1243600"
}
}
}
'''
loaded_data = json.loads(data)
time_series_data = loaded_data["Time Series (Daily)"]
temp = "^KS11"
for date, date_data in time_series_data.items():
formatted_date = date.replace("-","")
low_data = date_data["3. low"]
open_data = date_data["1. open"]
high_data = date_data["2. high"]
close_data = date_data["4. close"]
print("{}\t|{}|{}\t|{}\t|{}\t|{}\t|\n".format(temp, formatted_date, low_data, open_data, high_data, close_data))
assuming you know how to read json using json module.
stocks = {
"Time Series (Daily)": {
"2020-04-02": {
"1. open": "1693.5300",
"2. high": "1726.7600",
"3. low": "1664.1300",
"4. close": "1724.8600",
"5. volume": "766313"
},
"2020-04-01": {
"1. open": "1737.2800",
"2. high": "1762.4399",
"3. low": "1685.3700",
"4. close": "1685.4600",
"5. volume": "1243600"
}
}
}
for k,v in stocks['Time Series (Daily)'].items():
print("^KS11\t|{}\t|{}\t|{}\t|{}\t|{}|".format(v['5. volume'],v['3. low'],v['1. open'],v['2. high'],v['4. close']))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.