1

I have a simple Json file

input.json

[
{
    "title": "Person",
    "type": "object",
    "required": "firstName",
    "min_max": "200/600"
},
{
    "title": "Person1",
    "type": "object2",
    "required": "firstName1",
    "min_max": "230/630"
},
{
    "title": "Person2",
    "type": "object2",
    "required": "firstName2",
    "min_max": "201/601"
},
{
    "title": "Person3",
    "type": "object3",
    "required": "firstName3",
    "min_max": "2000/6000"
},
{
    "title": "Person4",
    "type": "object4",
    "required": "firstName4",
    "min_max": "null"
},
{
    "title": "Person4",
    "type": "object4",
    "required": "firstName4",
    "min_max": "1024 / 256"
},

{
    "title": "Person4",
    "type": "object4",
    "required": "firstName4",
    "min_max": "0"
}

]

I am trying to create a new json file with new data. I would like to split "min_max" into two different fields ie., min and max. Below is the code written in python.

import json
input=open('input.json', 'r')
output=open('test.json', 'w')
json_decode=json.load(input)

result = []
for item in json_decode:
    my_dict={}
    my_dict['title']=item.get('title')
    my_dict['min']=item.get('min_max')
    my_dict['max']=item.get('min_max')
    result.append(my_dict)

data=json.dumps(result, output)
output.write(data)
output.close()

How do I split the string into two different values. Also, is there any possibility of printing the json output in order.

3 Answers 3

1

Your JSON file seems to be written wrong (the example one). It is not a list. It is just a single associated array (or dictionary, in Python). Additionally, you don't seem to be using json.dumps properly. It only takes 1 argument. I also figured it would be easier to just create the dictionary inline. And you don't seem to be splitting the min_max properly.

Here's the correct input:

[{
    "title": "Person",
    "type": "object",
    "required": "firstName",
    "min_max": "20/60"
}]

Here's your new code:

import json

with open('input.json', 'r') as inp, open('test.json', 'w') as outp:
    json_decode=json.load(inp)    
    result = []
    for temp in json_decode:
        minMax = temp["min_max"].split("/")
        result.append({
            "title":temp["title"],
            "min":minMax[0],
            "max":minMax[1]
        })    
    data=json.dumps(result)
    outp.write(data)
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you @neil. It is showing "IndexError: list index out of range". I believe this is because my json file is too big. How can i overcome this?
@neil - I just edited the json data in my question. It is just an instance of data. I have more and more similar data.
You would have to put commas in between each one. I don't know how the data looks without looking at it.
@neil - what commas you are reffering to?. Also, I don't have any specific data. Just imagine the same data with more and more dictionaries. Thank you.
It sounds to me like you have a min_max value that doesn't have a / in it, which is causing it to run into trouble when attempting to reference the values in the minMax array from the .split('/') call. You will need to check to make sure that len(minMax) == 2 before attempting to add it to the result - and if it is not, add in code that does what you want to do in that case.
|
0

Table + Python == Pandas

import pandas as pd

# Read old json to a dataframe
df = pd.read_json("input.json")

# Create two new columns based on min_max
# Removes empty spaces with strip()
# Returns [None,None] if length of split is not equal to 2
df['min'], df['max'] = (zip(*df['min_max'].apply
                        (lambda x: [i.strip() for i in x.split("/")] 
                         if len(x.split("/"))== 2 else [None,None])))

# 'delete' (drop) min_max column
df.drop('min_max', axis=1, inplace=True)

# output to json again
df.to_json("test.json",orient='records')

Result:

[{'max': '600',
 'min': '200',
  'required': 'firstName',
  'title': 'Person',
  'type': 'object'},
 {'max': '630',
  'min': '230',
  'required': 'firstName1',
  'title': 'Person1',
  'type': 'object2'},
 {'max': '601',
  'min': '201',
  'required': 'firstName2',
  'title': 'Person2',
  'type': 'object2'},
 {'max': '6000',
  'min': '2000',
  'required': 'firstName3',
  'title': 'Person3',
  'type': 'object3'},
 {'max': None,
  'min': None,
 ...

Comments

0

You can do something like this:

import json 

nl=[]
for di in json.loads(js):
    min_,sep,max_=map(lambda s: s.strip(), di['min_max'].partition('/'))
    if sep=='/':
        del di['min_max']
        di['min']=min_
        di['max']=max_
    nl.append(di)

print json.dumps(nl)    

This keeps the "min_max" values that cannot be separated into two values unchanged.

Comments

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.