Python Forum
Convert string to JSON using a for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert string to JSON using a for loop
#1
Hello,

I need to transform a string like "Name1:Val1|Name2:Val2" to a JSON like [{'name': 'Name1', 'value': 'Val1', 'status': 'Passed'}, {'name': 'Name2', 'value': 'Val2', 'status': 'Passed'}].
When I run my code the result is [{'name': 'Name2', 'value': 'Val2', 'status': 'Passed'}, {'name': 'Name2', 'value': 'Val2', 'status': 'Passed'}].
I get {'name': 'Name1', 'value': 'Val1', 'status': 'Passed'} from the first loop execution. But it the second loop it is replaced by {'name': 'Name2', 'value': 'Val2', 'status': 'Passed'}.

My code is:

import json group={} tests=[] currentdata={} str="Name1:Val1|Name2:Val2" print(str) print("------------------------------") splitstr=str.split("|") print(splitstr) print("------------------------------") for dataset in splitstr: data=dataset.split(":") currentdata["name"]=data[0] currentdata["value"]=data[1] currentdata["status"]="Passed" tests.append(currentdata) print(data) print(data[0]) print(data[1]) print(currentdata) print(tests) print("------------------------------")
The output is:
Output:
Name1:Val1|Name2:Val2 ------------------------------ ['Name1:Val1', 'Name2:Val2'] ------------------------------ ['Name1', 'Val1'] Name1 Val1 {'name': 'Name1', 'value': 'Val1', 'status': 'Passed'} [{'name': 'Name1', 'value': 'Val1', 'status': 'Passed'}] ------------------------------ ['Name2', 'Val2'] Name2 Val2 {'name': 'Name2', 'value': 'Val2', 'status': 'Passed'} [{'name': 'Name2', 'value': 'Val2', 'status': 'Passed'}, {'name': 'Name2', 'value': 'Val2', 'status': 'Passed'}]
------------------------------

Thanks for your help.
buran write Jan-08-2021, 05:56 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Thank you, I will do it properly next time.
Reply
#3
dict is mutable object. You define currentdata before the loop. Basically you add same object multiple times. When you change value for particular key inside the loop, it change all objects inside the dict.
move line 5 inside the loop.

Also, don't use str as name, it's a built-in function.
PG_Breizh likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Many thanks, buran, for fixing this beginner issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert JSON to CSV Linuxdesire 6 7,790 Oct-15-2025, 12:05 AM
Last Post: Pedroski55
  Convert any Python expression to a string voidtrance 2 1,077 Jun-23-2025, 07:06 AM
Last Post: DeaD_EyE
  How to convert while loop to for loop in my code? tatahuft 4 1,614 Dec-21-2024, 07:59 AM
Last Post: snippsat
  Convert Json to table formathttps://python-forum.io/thread-38313.html python_student 3 18,485 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  get JSON string from URL with Windows credentials shwfgd 0 1,068 Aug-27-2024, 10:08 PM
Last Post: shwfgd
  Python Script to convert Json to CSV file chvsnarayana 8 6,297 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 7,412 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [split] Parse Nested JSON String in Python mmm07 4 3,724 Mar-28-2023, 06:07 PM
Last Post: snippsat
  convert string to float in list jacklee26 6 4,471 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert this List Comprehensions to loop jacklee26 8 3,944 Oct-21-2022, 04:25 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.