1

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  |
2
  • What does ^KS11 means? Commented Apr 2, 2020 at 14:39
  • 1
    I found your sequence in each dict of time is unordered.Is there any requirements? Commented Apr 2, 2020 at 15:07

4 Answers 4

2

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|
Sign up to request clarification or add additional context in comments.

1 Comment

How to add space each column? because I need to fix the digits like ^KS11 |20200402+add 4 space|1693.5300+add 4 space|1726.7600+add 4 space|1664.1300+add 4 space|1724.8600+add 4 space also is it possible add one more column for difference number between 20200402 open and 20200401 open?
1
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))

1 Comment

How to add space each column? because I need to fix the digits like ^KS11 |20200402+add 4 space|1693.5300+add 4 space|1726.7600+add 4 space|1664.1300+add 4 space|1724.8600+add 4 space also is it possible add one more column for difference number between 20200402 close and 20200401 close?
1

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)

Comments

-1

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']))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.