Python Forum
Looping JSON data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Looping JSON data (/thread-19484.html)



Looping JSON data - graham23s - Jul-01-2019

Hi Guys,

I'm trying to loop some JSON which i have created using PHP, sample:

{
    "action": [
        "val1|0|0|val4",
        "val1|val2|0|val4"
    ]
}
I seperate the values using a pipe | which i will then split once i get each line looped, i have so far (very basically):

y = json.loads(_json)
    for line in _json.split(y):
        print(y)
I'm not sure the best weay to go about this, any help would be appreciated.

regards

Graham


RE: Looping JSON data - nilamo - Jul-01-2019

(Jul-01-2019, 08:04 PM)graham23s Wrote: for line in _json.split(y):
If _json is a string, that line should be giving an error. I don't understand what you're trying to accomplish with that, since y is a dict. You should be able to just iterate over the actions directly:
y = json.loads(_json)
if "action" in y:
    for action in y["action"]:
        parts = action.split("|")
        print(parts)