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

This is a question on forming a template #95

Open
rob2701 opened this issue Apr 23, 2019 · 1 comment
Open

This is a question on forming a template #95

rob2701 opened this issue Apr 23, 2019 · 1 comment

Comments

@rob2701
Copy link

rob2701 commented Apr 23, 2019

Hello, this is a question on forming a template.

My input is :
termMarks is an array and its value can change, can be just 1 or a maximum of 4

{
"id" : "123",
"subject": [{
"name": "math"
"termMarks": [
"test1","test2","test3"
]
}]
}

Desired Output for above input :
{
"id": "123"
"terms": [
{}, {},{}
] }

Basing on the size of the termMarks array my output json should change. In the above input my "termMarks" size is 3 so I wanted the "terms" array in the output to be 3.

can someone help with a template

@au2
Copy link
Member

au2 commented May 2, 2019

@rob2701, unfortunately there is no easy way to reduce arrays of arrays to a single array if you also want properties that are not in the array. In your example "name". If you only want marks themselves you can try to use jsonpath expressions

var jsonave = require('jsonave');
{
    content: {
        id: {
            dataKey: "id"
        },
        terms: {
            content: {
                 mark: {
                }
            },
            dataKey: jsonave.instance('subject[\*].termMarks[\*]')
        }
    }
}

This will give

{
    "id": "123",
    "terms": [
        {
            "mark": "test1"
        },
        {
            "mark": "test2"
        },
        {
            "mark": "test3"
        }
    ]
}

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

2 participants