Mapping a CSV field to Array of Objects

This topic has 4 replies, 2 voices, and was last updated 3 months, 2 weeks ago by ak.tokas.

  • Author
    Posts
  • #26598
     XavierForde
    Participant

    Hello,

    I have created a new property in managed/Users which is an array of objects as follows.

    "addressesArrayObject" : {
    
                            "title" : "Array of Address Objects",
    
                            "type" : "array",
    
                            "viewable" : true,
    
                            "searchable" : false,
    
                            "userEditable" : true,
    
                            "items" : {
    
                                "type" : "object",
    
                                "properties" : {
    
                                    "post_code" : {
    
                                        "title" : null,
    
                                        "type" : "string",
    
                                        "viewable" : true,
    
                                        "searchable" : true,
    
                                        "userEditable" : true
    
                                    },
    
                                    "city" : {
    
                                        "title" : null,
    
                                        "type" : "string",
    
                                        "viewable" : true,
    
                                        "searchable" : true,
    
                                        "userEditable" : true
    
                                    }
    
                                },
    
                                "order" : ,
    
                                "required" :
    
                            },
    
                            "description" : null,
    
                            "isVirtual" : false
    
                        },

    So the the property is an array of address objects which have the properties ‘post_code’ and ‘city’. In the CSV file I have one user and the address is a string stored as follows (quoteCharaceter = ‘):

    ‘{“post_code”:”LU4TTT”,”city”:”Lyon”};{“post_code”:”PA4LTT”,”city”:”Paris”}’

    So far I can load this into an array of two JSON strings using the JS code to transform to an array:
    source.split(";")
    I am unsuccessful in trying to do the next step of creating array of objects. The CSV JSON string keys are the same as defined in the schema so I though it may be as simple as the following JS code:
    source.split(";").map(x => JSON.parse(x))

    Here is the sync.json code for the mapping to the Array of strings (which works) and the Array of objects (doesn’t work):

    
                    {
    
                        "target" : "addressesArray",
    
                        "transform" : {
    
                            "type" : "text/javascript",
    
                            "globals" : { },
    
                            "source" : "source.split(";")"
    
                        },
    
                        "source" : "addresslist"
    
                    },
    
                    {
    
                        "target" : "addressesArrayObject",
    
                        "source" : "addresslist",
    
                        "transform" : {
    
                            "type" : "text/javascript",
    
                            "globals" : { },
    
                            "source" : "source.split(";").map(x => JSON.parse(x))"
    
                        }
    
                    }

    I realise this is a basic question but I’ve been struggling to find mapping to an object for a while now. Any help greatly appreciated :)

    #26608
     XavierForde
    Participant

    Problem was I needed to require the lodash library to perform mapping. The following code solved the problem:

    var _ = require('lib/lodash');
     _.map(source.split(";"), function(address) { return JSON.parse(address) });
    #27348
     Kumar
    Participant

    Could you please share the relevant entries from sync.json?

    #27349
     Kumar
    Participant

    Thanks, No worry, Xavier.

    the following Javascript code doesn’t work for me.

    var _ = require(‘lib/lodash’);
    _.map(source.split(“;”), function(address) { return JSON.parse(address) });

    However, I have manged to achieve via Groovy code and able to reconcile mulit-values(Array of Objects) via CSV Connector successfully .

    #27747
     ak.tokas
    Participant

    Hi Guys,

    Is there any way to map the Array’s sub attributes separately??

    For example,

    if address array has attributes line 1 and line 2

    Could they be mapped seaparately in the sync.json file instead of pushing the whole addressarray object??

    Regards,
    Akshay

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

©2020 ForgeRock - we provide an identity and access platform to secure every online relationship for the enterprise market, educational sector and even entire countries. Click to view our privacy policy and terms of use.

Log in with your credentials

Forgot your details?