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


Messages In This Thread
Convert string to JSON using a for loop - by PG_Breizh - Jan-08-2021, 05:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Script to convert Json to CSV file chvsnarayana 8 2,719 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,347 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [split] Parse Nested JSON String in Python mmm07 4 1,657 Mar-28-2023, 06:07 PM
Last Post: snippsat
  convert string to float in list jacklee26 6 2,045 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert this List Comprehensions to loop jacklee26 8 1,639 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,527 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert Json to table format python_student 2 6,059 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert string to float problem vasik006 8 3,557 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert nested sample json api data into csv in python shantanu97 3 3,030 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,711 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