0
        data={}
        data['intents']=[]
        data['intents'].append({
            'tag': tag,
            'patterns': patterns,
            'response': response
        })
        with open('training.json', 'a') as training:

            json.dump(data, training)

I am trying to append values to the key intents. but when I am trying to append values i am getting output as follows:

{"intents": [{"response": "customize", "patterns": "erp", "tag": "purchase"}]}{"intents": [{"response": "kjj", "tag": "sales", "patterns": "jjkj"}]}

i want my output in the format given below:

{"intents":[
        {"tag":"sale",
         "patterns":["ptr1","ptr2"],
         "responses":["resp1","resp2"]
        },
        {"tag":"purchase",
         "patterns":["abc","def"],
         "responses":["xyz","zzz"]
        }
    ]
}
2
  • Why do you care about key order? Serialized languages aren't meant to be used for display. Commented Nov 22, 2018 at 5:41
  • actually, my concern is not about intent. I need to append values to the key indents. but when I try to do so the key gets repeated. i.e what I showed here. can you please tell me a way append key-value pair to a single key. @TheIncorrigible1 Commented Nov 22, 2018 at 6:20

3 Answers 3

1

You cannot append new data it will break your json, you have to replace data. And dont focus on indent, its not necessary try this:

import json
data={}
data['intents']=[]
data['intents'].append({
    'tag': 'tag',
    'patterns': 'patterns',
    'response': 'response'
})
try:
    with open('training.json', 'r') as training:
        old_data = training.readlines()
        if old_data:
            old_data = json.loads(old_data[0])
            for intents in data['intents']:
                old_data['intents'].append(intents)
            data = old_data
            old_data = None
    with open('training.json', 'w') as training:
        json.dump(data, training)
except:
    with open('training.json', 'w') as training:
        json.dump(data, training)
Sign up to request clarification or add additional context in comments.

Comments

0

As you want to append an item to a list in the object (and not append text to the json file itself), you need to first read the json, then append and write:

import os

data = {'intents': []}
if os.path.exists('training.json'):
    with open('training.json', 'r') as f:
        data = json.load(f)

data['intents'].append({
            'tag': tag,
            'patterns': patterns,
            'response': response
})

with open('training.json', 'w') as training:
    json.dump(data, training)

2 Comments

actually, my concern is not about intent. I need to append values to the key indents. but when I try to do so the key gets repeated. i.e what I showed here. can you please tell me a way append key-value pair to a single key.
neha check my answer, you will get your desired output
0

Well you can add it without copying the contents from old JSOF file easily.

    import json

    data={}
    data['intents']=[]
    data['intents'].append({
        'tag': 'tag',
        'patterns': 'patterns',
        'responses': 'responses'
    })

    tag = request.json['tag']
    new_response = request.json['response']
    for intent in intents['intents']:
        if intent['tag'] in tag:
            if new_response in intent['responses']:
                response = {"success":False,"message":"Response already found in the training data"}
                return response
            else:
                
                print("updating file")
                intent['responses'].append(new_response)
                data = intents
                with open('intents.json', 'w') as training:
                    json.dump(data, training)  

This will append the new response under the same tag. Also if the same response is present in it, then it will skip.

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.