I want to convert the below json format into another json

Discussion in 'Javascript' started by newcoder, May 5, 2020.

  1. newcoder

    newcoder

    Joined:
    Feb 27, 2020
    Messages:
    5
    Likes Received:
    0
    Code (Javascript):

    {
        "outputs": [
            {
                "name": "U1A_SHIFT",
                "pressure": "VFM.U_FIELD.U1A_SHIFT.DHPT.pressure",
                "temperature": "VFM.U_FIELD.U1A_SHIFT.DHPT.temperature",
                "volFlowLC_o": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowLC_o",
                "volFlowLC_w": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowLC_w",
                "volFlowLC_g": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowLC_g",
                "volFlowSC_o": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowSC_o",
                "volFlowSC_w": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowSC_w",
                "volFlowSC_g": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowSC_g",
                "massFlowLC_o": "VFM.U_FIELD.U1A_SHIFT.DHPT.massFlowLC_o",
                "massFlowLC_w": "VFM.U_FIELD.U1A_SHIFT.DHPT.massFlowLC_w",
                "massFlowLC_g": "VFM.U_FIELD.U1A_SHIFT.DHPT.massFlowLC_g",
                "massFlowSC_o": "VFM.U_FIELD.U1A_SHIFT.DHPT.massFlowSC_o",
                "massFlowSC_w": "VFM.U_FIELD.U1A_SHIFT.DHPT.massFlowSC_w",
                "massFlowSC_g": "VFM.U_FIELD.U1A_SHIFT.DHPT.massFlowSC_g"
            },
            {
                "name": "U1A_SHIFT_UPT",
                "pressure": "VFM.U_FIELD.U1A_SHIFT.UPT.pressure",
                "temperature": "VFM.U_FIELD.U1A_SHIFT.UPT.temperature"
            },
            {
                "name": "U1A_SHIFT_PCV_UPT",
                "pressure": "VFM.U_FIELD.U1A_SHIFT.PCV_UPT.pressure",
                "temperature": "VFM.U_FIELD.U1A_SHIFT.PCV_UPT.temperature"
            },{
                "name": "U3B_PCV_DPT",
                "pressure": "VFM.U_FIELD.U3B.PCV.DPT.pressure",
                "temperature": "VFM.U_FIELD.U3B.PCV.DPT.temperature",
                "volFlowLC_o": "VFM.U_FIELD.U3B.PCV.DPT.volFlowLC_o",
                "volFlowLC_w": "VFM.U_FIELD.U3B.PCV.DPT.volFlowLC_w",
                "volFlowLC_g": "VFM.U_FIELD.U3B.PCV.DPT.volFlowLC_g",
                "volFlowSC_o": "VFM.U_FIELD.U3B.PCV.DPT.volFlowSC_o",
                "volFlowSC_w": "VFM.U_FIELD.U3B.PCV.DPT.volFlowSC_w",
                "volFlowSC_g": "VFM.U_FIELD.U3B.PCV.DPT.volFlowSC_g",
                "massFlowLC_o": "VFM.U_FIELD.U3B.PCV.DPT.massFlowLC_o",
                "massFlowLC_w": "VFM.U_FIELD.U3B.PCV.DPT.massFlowLC_w",
                "massFlowLC_g": "VFM.U_FIELD.U3B.PCV.DPT.massFlowLC_g",
                "massFlowSC_o": "VFM.U_FIELD.U3B.PCV.DPT.massFlowSC_o",
                "massFlowSC_w": "VFM.U_FIELD.U3B.PCV.DPT.massFlowSC_w",
                "massFlowSC_g": "VFM.U_FIELD.U3B.PCV.DPT.massFlowSC_g"
            },
        ]
    }

    ===================
    [B]convert to below fromat[/B]
    =================

    {
    "namespace": 2,
    "channels": [{
                "channelId": "VFM",
                "devices": [{
                "deviceId": "U_FIELD",
                "tagGroup": [{
                        "tagGroupId": "U1A_SHIFT",
                        "tagGroup": [
                            {
                                "tagGroupId": "DHPT",
                                "tags": [
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.pressure",
                                        "value": 10
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.temperature",
                                        "value": 20
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowLC_o",
                                        "value": 30
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowLC_w",
                                        "value": 40
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.volFlowLC_g",
                                        "value": 50
                                    }
                                ]
                            },
                            {
                                "tagGroupId": "PCV_UPT",
                                "tags": [
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.PCV_UPT.pressure",
                                        "value": 10
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.PCV_UPT.temperature",
                                        "value": 20
                                    }
                                ]
                            },
                            {
                                "tagGroupId": "UPT",
                                "tags": [
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.UPT.pressure",
                                        "value": 10
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U1A_SHIFT.UPT.temperature",
                                        "value": 20
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "tagGroupId": "U3B",
                        "tagGroup": [
                            {
                            "tagGroupId": "PCV",
                            "tagGroup": [{
                                "tagGroupId": "DPT",
                                "tags": [
                                    {
                                        "tagId": "VFM.U_FIELD.U3B.PCV.DPT.pressure",
                                        "value": 10
                                    },
                                    {
                                        "tagId": "VFM.U_FIELD.U3B.PCV.DPT.temperature",
                                        "value": 20
                                    }
                                ]
                            }]
                        }]
                    }]
    }]
    }]
    }
     
     
    Last edited by a moderator: May 5, 2020
    newcoder, May 5, 2020
    #1
    1. Advertisements

  2. newcoder

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    102
    Likes Received:
    12
    Nowhere in the first JSON do you have the values that you want to place into the second JSON.

    The secondJSON can be written without the first. Just write it. To find the values that go into the second
    Code (Text):

    {
    "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.pressure",
    "value": ?
    },
     
    you just need to inquire for the value of "VFM.U_FIELD.U1A_SHIFT.DHPT.pressure" OR "pressure"
     
    sunfighter, May 5, 2020
    #2
    1. Advertisements

  3. newcoder

    newcoder

    Joined:
    Feb 27, 2020
    Messages:
    5
    Likes Received:
    0
    There is one more JSON for the value so based on name need to search for particular tags and fetch the value for key

    Note: It support vanilla JS

    For eg,
    {
    "tagId": "VFM.U_FIELD.U1A_SHIFT.DHPT.pressure",
    "value": 22134912.41624013
    },

    =================

    Value JSON:
    {
    nodeEstimates: [{
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U1A_SHIFT",
    "pressure": 22134912.41624013,
    "temperature": 36.5964997757411,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0

    }, {
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U1A_SHIFT_UPT",
    "pressure": 22134803.48582023,
    "temperature": 36.5964997757411,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0
    }, {
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U1A_SHIFT_PCV_UPT",
    "pressure": 9487835.499685265,
    "temperature": 59.89809807115756,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0
    }, {
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U3B_PCV_DPT",
    "pressure": 9534927.911307203,
    "temperature": 4.136886670720397,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0
    }]
    }
     
    newcoder, May 6, 2020
    #3
  4. newcoder

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    102
    Likes Received:
    12
    newcoder please pay attention to what and how you code things. In the above code titled 'Value JSON:' you:
    A.) You reverse the second and third groups
    B.) The first group name is U1A_SHIFT. S/B U1A_SHIFT.DHPT.
    C.) The names you use are confusing. In the last group (picked because it's easy to see) you have:
    "name": "U3B_PCV_DPT",
    and in the file you want to make you use:
    "tagId": "VFM.U_FIELD.U3B.PCV.DPT.pressure",
    The difference is a dot in one and an _ in the other.
    -----------------------------------------------

    I can only guess this is an exercise, because -- well just watch.
    Here's how to find the readings
    Code (Text):

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>MINE</title>
    </head>


    <body>
    <p id="demo">demo</p>
    <p id="demo1">demo</p>
    <p id="demo2">demo</p>

    <script>
    var obj = {
    nodeEstimates: [{
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U1A_SHIFT",
    "pressure": 22134912.41624013,
    "temperature": 36.5964997757411,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0

    }, {
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U1A_SHIFT_UPT",
    "pressure": 22134803.48582023,
    "temperature": 36.5964997757411,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0
    }, {
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U1A_SHIFT_PCV_UPT",
    "pressure": 9487835.499685265,
    "temperature": 59.89809807115756,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0
    }, {
    "massFlowLC_g": 0.0,
    "massFlowLC_o": 0.0,
    "massFlowLC_w": 0.0,
    "massFlowSC_g": 0.0,
    "massFlowSC_o": 0.0,
    "massFlowSC_w": 0.0,
    "name": "U3B_PCV_DPT",
    "pressure": 9534927.911307203,
    "temperature": 4.136886670720397,
    "volFlowLC_g": 0.0,
    "volFlowLC_o": 0.0,
    "volFlowLC_w": 0.0,
    "volFlowSC_g": 0.0,
    "volFlowSC_o": 0.0,
    "volFlowSC_w": 0.0
    }]
    }

    document.getElementById("demo").innerHTML = 'This is the pressure : ' + 'For ' + obj['nodeEstimates'][2]['name'] + ' : ' + obj['nodeEstimates'][2]['pressure'] +
    '<br>  This is the temperature : '+ obj['nodeEstimates'][2]['temperature'];
    document.getElementById("demo1").innerHTML = 'This is the pressure : ' + 'For ' + obj['nodeEstimates'][1]['name'] + ' : ' +  obj['nodeEstimates'][1]['pressure'] +
    '<br>  This is the temperature : '+ obj['nodeEstimates'][1]['temperature'];
    document.getElementById("demo2").innerHTML = 'This is the pressure : ' +  'For ' + obj['nodeEstimates'][3]['name'] + ' : ' +  obj['nodeEstimates'][3]['pressure'] +
    '<br>  This is the temperature : '+ obj['nodeEstimates'][3]['temperature'];
    </script>
    </body>
    </html>
     
     
    sunfighter, May 11, 2020
    #4
  5. newcoder

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    102
    Likes Received:
    12
    To find the value of the
    VFM.U_FIELD.U1A_SHIFT.UPT.pressure
    in the change to code we need to use this
    str.channels[0].devices[0].channelId[0].tagGroup[2].tags[0].value;
    (str is the var I set the code equal to)
    The reason this is so complicated is because you have JSON (the code inside of curly brackets {...}
    inside of arrays (denoted by square brackets [].
    -----------------------------

    A far easier method would be to just send the data in a JSON package:
    Code (Text):

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>MINE</title>
    </head>


    <body>
    <p id="demo">demo</p>
    <p id="demo1">demo</p>
    <p id="demo2">demo</p>

    <script>
    var yxt = '{"DHPT_pressure": 10, "DHPT_temperature":23, "UPT_pressure":290, "UPT_temperature":19,"UPT_pressure":53,"UPT_temperature":17,"DPT_pressure":5, "DPT_temperature":77}';

    var myObj = JSON.parse(yxt);

    document.getElementById("demo").innerHTML = myObj.UPT_pressure;
    document.getElementById("demo2").innerHTML = myObj.DPT_temperature;

    </script>
    </body>
    </html>
     
     
    sunfighter, May 11, 2020
    #5
  6. newcoder

    newcoder

    Joined:
    Feb 27, 2020
    Messages:
    5
    Likes Received:
    0
    Thanks for your help:)
     
    newcoder, May 11, 2020
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.