0

After running a GET API call, I am presented with the following output:

{
    "limit": 1000,
    "offset": 0,
    "records": [{
        "associatedItems": [{
            "id": "rk:db",
            "name": "rk:db",
            "parentId": "10000",
            "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory",
            "typeName": "USER"
        }],
        "category": "group management",
        "created": "2022-10-13T17:44:01.633+0000",
        "eventSource": "",
        "id": 42914,
        "objectItem": {
            "name": "system-administrators",
            "parentId": "10000",
            "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory",
            "typeName": "GROUP"
        },
        "summary": "User added to group"
    }, {
        "associatedItems": [{
            "id": "rk:da",
            "name": "rk:da",
            "parentId": "10000",
            "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory",
            "typeName": "USER"
        }],
        "category": "group management",
        "created": "2022-10-13T17:44:01.610+0000",
        "eventSource": "",
        "id": 42913,
        "objectItem": {
            "name": "site-admins",
            "parentId": "10000",
            "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory",
            "typeName": "GROUP"
        }
    }],
    "total": 492
}

I want to extract each element from the "records" array to create multiple JSON objects.
One (1) JSON object per array element.

For example: JSON Object #1

{
    "associatedItems": [{
        "id": "rk:db",
        "name": "rk:db",
        "parentId": "10000",
        "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory",
        "typeName": "USER"
    }],
    "category": "group management",
    "created": "2022-10-13T17:44:01.633+0000",
    "eventSource": "",
    "id": 42914,
    "objectItem": {
        "name": "system-administrators",
        "parentId": "10000",
        "parentName": "com.atlassian.crowd.directory.IdentityPlatformRemoteDirectory",
        "typeName": "GROUP"
    },
    "summary": "User added to group"
}

Ideally, I want to perform the JSON object manipulation using Python.

Can anyone help me achieve this goal? Thanks!

anengelsen
  • 73
  • 1
  • 10
  • What you meant by *create multiple JSON objects*? Storing into different file? – Rahul K P Oct 13 '22 at 22:10
  • Please [Take the Tour](https://stackoverflow.com/tour), read: [What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask), [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). What have you tried and how does it differ from your expectation? SO is not a "write code for me" service, but a community of developers who help each other solve specific problems. – Michael Ruth Oct 13 '22 at 22:10
  • If that JSON is parsed into a variable named data, use `for record in data['records']:` to iterate over the `records` array. – Barmar Oct 13 '22 at 22:20
  • See https://stackoverflow.com/questions/48193502/access-a-particular-field-in-arbitrarily-nested-json-data – Barmar Oct 13 '22 at 22:20
  • 1
    If the JSON output is stored in a string variable `s`, then `data=json.loads(s)` parses the JSON into a python dictionary, after which `data['records']` is an array of dictionaries, one per JSON object of interest. – Darryl Oct 13 '22 at 23:03
  • @Darryl I need the data in the "records" array to remain valid JSON. An array of python dictionaries is no longer considered valid JSON. (Because the key value pairs will be encapsulated in single quotes, instead of double quotes.) Please, correct me if I'm wrong. – anengelsen Oct 14 '22 at 01:47
  • An array of dictionaries IS python's in-code representation of a collection of JSON objects. The single vs double quotes thing is completely irrelevant. The keys are strings that don't have any quotes of either kind, the quotes are just added for display purposes. People were helping you with parsing and with understanding what you parsed, but I now see that your question was "how do I go from parsed JSON back to a JSON-formatted string?" – Darryl Oct 14 '22 at 19:38

1 Answers1

0

Not sure if this is "best practice". But I was able to convert the "records" array (nested inside of the GET api response) into a Python dictionary. I then used a FOR loop to convert each array element back into valid JSON.

jira_records = response.json()['records']
for record in jira_records:
    x = json.dumps(record)
    print(x)

The final result is 1 (valid) JSON object per array element--which is exactly what I needed.

If there is less roundabout way to accomplish this, I'de love to "learn more".

anengelsen
  • 73
  • 1
  • 10
  • Whether this is the best approach depends on what you want to do with the JSON. If you're manipulating the data it is best to keep it in dicts while using/changing the data. If you are sending the data to a stream, such as saving to a file, it is best to use `json.dump(record, f)` instead of converting it to a string first. If you are printing to the screen or doing something inherently string oriented, then `json.dumps(record)` is the way to go. – Darryl Oct 14 '22 at 19:52