0
[
{
    "builtin_name": "custom_template",
    "fields": [
        {
            "id": 10012,
            "field_type": "OBJECT_SET",
            "tooltip_text": "",
            "name_plural": "",
            "name_singular": "reference",
            "backref_name": "reference",
            "backref_tooltip_text": "",
            "allow_multiple": false,
            "allowed_otypes": [
                "schema",
                "table",
                "attribute",
                "user",
                "groupprofile",
                "groupprofile"
            ],
            "options": null,
            "builtin_name": null
        },
        {
            "id": 8,
            "field_type": "OBJECT_SET",
            "tooltip_text": null,
            "name_plural": "Stewards",
            "name_singular": "Steward",
            "backref_name": "Steward",
            "backref_tooltip_text": null,
            "allow_multiple": true,
            "allowed_otypes": ["user", "groupprofile", "groupprofile"],
            "options": null,
            "builtin_name": "steward"
        }
    ],
    "id": 16,
    "title": "Custom template"
},
{
    "builtin_name": "new_template",
    "fields": [
        {
            "id": 10011,
            "field_type": "PICKER",
            "tooltip_text": "",
            "name_plural": "",
            "name_singular": "status",
            "backref_name": "",
            "backref_tooltip_text": "",
            "allow_multiple": false,
            "allowed_otypes": null,
            "options": [
                {
                    "old_index": 0,
                    "article_id": null,
                    "tooltip_text": null,
                    "in_use": true,
                    "title": "Done"
                }
            ],
            "builtin_name": null
        }
    ],
    "id": 1899,
    "title": "New Template"
}

] Using this JSON object, I want to replace the fields list where id=16 and title=Custom template. I have already generated the object I want to use to the replacement.

Here's what I have tried, but didn't work:

i=0    
jdata_copy = jdata #this is so I can compare
for t in jdata:
    if(t["title"]=="Custom template" and t["id"]==16):            
        jdata_copy[i]["fields"] = repl_data
    i+=1 

I've looked around for a simple json replace but haven't found.

Appreciate some help.

Thanks!

1 Answer 1

1

Ok, assuming this the python script is in the same directory as a json file called test.json and this is what the json looks like (not the same as yours since yours has both null and None values for some reason so I wasn't sure what to do with it):

[
    {
        "builtin_name": "custom_template",
        "fields": [
            {
                "id": 10012,
                "field_type": "OBJECT_SET",
                "tooltip_text": "",
                "name_plural": "",
                "name_singular": "reference",
                "backref_name": "reference",
                "backref_tooltip_text": "",
                "allow_multiple": false,
                "allowed_otypes": [
                    "schema",
                    "table",
                    "attribute",
                    "user",
                    "groupprofile",
                    "groupprofile"
                ],
                "options": null,
                "builtin_name": null
            },
            {
                "id": 8,
                "field_type": "OBJECT_SET",
                "tooltip_text": null,
                "name_plural": "Stewards",
                "name_singular": "Steward",
                "backref_name": "Steward",
                "backref_tooltip_text": null,
                "allow_multiple": true,
                "allowed_otypes": ["user", "groupprofile", "groupprofile"],
                "options": null,
                "builtin_name": "steward"
            }
        ],
        "id": 16,
        "title": "Custom template"
    },
    {
        "builtin_name": "new_template",
        "fields": [
            {
                "id": 10011,
                "field_type": "PICKER",
                "tooltip_text": "",
                "name_plural": "",
                "name_singular": "status",
                "backref_name": "",
                "backref_tooltip_text": "",
                "allow_multiple": false,
                "allowed_otypes": null,
                "options": [
                    {
                        "old_index": 0,
                        "article_id": null,
                        "tooltip_text": null,
                        "in_use": true,
                        "title": "Done"
                    }
                ],
                "builtin_name": null
            }
        ],
        "id": 1899,
        "title": "New Template"
    }
]

Then, this code works to load and change fields:

import json


with open("test.json") as json_file:
    jdata = json.load(json_file)

jdata_copy = jdata

for t in jdata: if t["id"] == 16 and t["title"] == "Custom template": pprint(jdata) print() t["fields"] = ["howdy", "hello"] print() pprint(jdata)

However let me know if this is the case for you, I wasn't sure from your question exactly what you were working with.

Edit

So, changing the code to:

import json
from pprint import pprint


with open("test.json") as json_file:
    jdata = json.load(json_file)

jdata_copy = jdata

for t in jdata:
    if t["id"] == 16 and t["title"] == "Custom template":
        pprint(jdata)

        t["fields"] = ["howdy", "hello"]

        pprint(jdata)

With this I can see that the first print statement returns the json as above just as a python dictionary, and the second print statement does indeed show jdata has had the fields section of the correct dictionary modified.

Then, I can export the dictionary back out to a .json:

with open("test1.json", "w") as json_file:
    json.dump(jdata, json_file)

This all works fine for me, which section here is not working for you? Sorry if I have misunderstood.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply. That doesn't work though. I had tried that.
Can you show how you are loading the json, and what is the json object etc. because the json data you provided above doesn't make much sense
I fixed and updated the original in my post above, it was an artifact of python that I didn't notice. Still doesn't make a difference about replacement though.
What do you mean by that though, after this what do you do with jdata? Do you export to json again?
Now that I've worked through it with your help, there was really no issue, it was my misunderstanding and I was replacing the wrong thing. Thanks for your help!
|

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.