Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The "$array" spec is not behaving as expected #12

Open
KilianLissSMRTR opened this issue Sep 4, 2024 · 0 comments
Open

The "$array" spec is not behaving as expected #12

KilianLissSMRTR opened this issue Sep 4, 2024 · 0 comments

Comments

@KilianLissSMRTR
Copy link

Here is some example code to reproduce the issue:

from json_converter.json_mapper import JsonMapper
from json_converter.json_mapper import json_object, json_array
from json_converter.post_process import prefix_with, concatenate_list
from json_converter.post_process import default_to
import json

exampleJson = {
    "STUDY_SET": {
        "STUDY": {
            "IDENTIFIERS": {
                "EXTERNAL_ID": [
                    {
                        "@namespace": "BioProject",
                        "@label": "primary",
                        "#text": "PRJNA269149"
                    },
                    {
                        "@namespace": "GEO",
                        "#text": "GSE63817"
                    }
                ]
            },
            "STUDY_LINKS": {
                "STUDY_LINK": {
                    "XREF_LINK": {
                        "DB": "pubmed",
                        "ID": "26495981"
                    }
                }
            }
        }
    }
}

specs = {
    '$on' : 'STUDY_SET.STUDY',
    'schema:identifier' : ['$array', [
        {
            '$on' : 'IDENTIFIERS.EXTERNAL_ID',
            '@type': ['', default_to, 'PropertyValue'],
            'schema:propertyID': ['', default_to, 'external_id'],
            'schema:value': ['#text'],
            'schema:valueReference' : ['@namespace']
        },
        {
            '@type': ['', default_to, 'PropertyValue'],
            'schema:propertyID': ['STUDY_LINKS.STUDY_LINK.XREF_LINK.DB'],
            'schema:value': ['STUDY_LINKS.STUDY_LINK.XREF_LINK.ID']
        },
    ], True],  # Keep this True for array mapping
}

transformedJSON = JsonMapper(exampleJson).map(specs)
print(json.dumps(transformedJSON, indent=4))

This is what I would expect it to output:

{
    "schema:identifier": [
        {
            "@type": "PropertyValue",
            "schema:propertyID": "external_id",
            "schema:value": "PRJNA269149",
            "schema:valueReference": "BioProject"
        },
        {
            "@type": "PropertyValue",
            "schema:propertyID": "external_id",
            "schema:value": "GSE63817",
            "schema:valueReference": "GEO"
        },
        {
            "@type": "PropertyValue",
            "schema:propertyID": "pubmed",
            "schema:value": "26495981"
        }
    ]
}

However it outputs with missing variables and values:

{
    "schema:identifier": [
        {
            "@type": "PropertyValue",
            "schema:propertyID": "external_id"
        },
        {
            "@type": "PropertyValue",
            "schema:propertyID": "pubmed",
            "schema:value": "26495981"
        }
    ]
}

I've tried a few alternative specs, but they either crash, or produce even more undesirable outputs.

Thanks in advanced for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant