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
  Python Script to convert Json to CSV file chvsnarayana 8 2,519 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,126 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [split] Parse Nested JSON String in Python mmm07 4 1,527 Mar-28-2023, 06:07 PM
Last Post: snippsat
  convert string to float in list jacklee26 6 1,913 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert this List Comprehensions to loop jacklee26 8 1,512 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,337 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert Json to table format python_student 2 5,518 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert string to float problem vasik006 8 3,400 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert nested sample json api data into csv in python shantanu97 3 2,816 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,520 May-13-2022, 07:28 PM
Last Post: mikepy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020