Python Forum

Full Version: Looping JSON data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
(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)