Krishna Prasad
Krishna Prasad

Reputation: 69

Splitting a string in json using python

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.

Upvotes: 1

Views: 14242

Answers (3)

dawg
dawg

Reputation: 104062

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.

Upvotes: 0

Anton vBR
Anton vBR

Reputation: 18916

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,
 ...

Upvotes: 0

Neil
Neil

Reputation: 14323

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)

Upvotes: 1

Related Questions