1

I need a json file in this order as i have to keep changing the json values. This is a sample file i want to generate

1.control_file Also the location name tag in the json should be enclosed in "","" format which i need help with.

{
 "user": "dex",
 "issue_no": "test_tkt",
 "start_date": "2017-07-01",
 "end_date": "2017-07-01",
 "geo-ids": [627,438,360],
 "location_names": [
    "India.v1.2a_Final",
    "China.v2.3a_setup",
    "Hongkong.4a"
 ]
}

Below is the code i wrote. The expected output

import json
import os


filename = input("Enter the file name: ")
user = input("Enter the user name: ")
ticket = input("Enter the ticket id: ")
start_date = input("Enter the start date: ")
end_date = input("Enter the end date: ")

test_list=[]
input_list=input("enter the exp names: ")
exports = input_list.split(",")
for names in exports:
        demo = test_list.append(names)
        print(demo)


if not os.path.splitext(filename)[1]:
    filename += ".txt"


with open(filename, 'w') as f:
    json.dump({
        "user": user,
        "ticket": ticket,
        "start_date": start_date,
        "end_date" : end_date,
        "location_names" : [demo]
      }, f, indent=4)


print("JSON saved to file {}".format(os.path.abspath(filename)))

output i get

{
    "user": "dex",
    "ticket": "test_tkt",
    "start_date": "2017-07-01",
    "end_date": "2017-07-01",
    "location_names": [
        None
        None
        None
    ]
}

Output i want:

{
 "user": "dex",
 "issue_no": "test_tkt",
 "start_date": "2017-07-01",
 "end_date": "2017-07-01",
 "location_names": [
    "India.v1.2a_Final",
    "China.v2.3a_setup",
    "Hongkong.4a"
 ]
}

2 Answers 2

2

You're assigning the return value of test_list.append() to the variable demo, but that will always be None because append() doesn't return anything.

Since test_list contains the list of names, you should use that as the value and get rid of the demo variable:

with open(filename, 'w') as f:
    json.dump({
        "user": user,
        "ticket": ticket,
        "start_date": start_date,
        "end_date" : end_date,
        "location_names" : test_list  # Use test_list
      }, f, indent=4)
Sign up to request clarification or add additional context in comments.

Comments

0

use this instead.

with open(filename, 'w') as f:
json.dump({
    "user": user,
    "ticket": ticket,
    "start_date": start_date,
    "end_date" : end_date,
    "location_names" : [item for item in demo]
  }, f, indent=4)

1 Comment

Enter the file name: test3 Enter the user name: dex Enter the ticket id: LIN-0000 Enter the start date: 2017-07-01 Enter the end date: 2017-07-01 enter the exp names: India.v1.2a_Final,China.v2.3a_setup,Hongkong.4a" ]None None None Traceback (most recent call last): File "C:\Users\dex\eclipse-workspace\PythonTest\src\SamplePrac\Custom_json.py", line 44, in <module> "export_names" : [item for item in demo] TypeError: 'NoneType' object is not iterable Getting this error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.