Jump to content

Build JSON with this structure


Recommended Posts

Posted (edited)

Hello

I need to create a json in the following structure:

{
    "Value1": {
        "Build": {
            "1": {
                "t_inst": {
                    "Type": "Gold",
                    "Key": "15",
                    "size": "big"
                },
                "1_avg": {
                    "Type": "Green",
                    "Key": "11",
                    "size": "big"
                },
                "2_avg": {
                    "Type": "Blue",
                    "Key": "21",
                    "size": "big"
                }
            },
            "2": {
                "t_inst": {
                    "Type": "Gold",
                    "Key": "15",
                    "size": "big"
                },
                "1_avg": {
                    "Type": "Green",
                    "Key": "11",
                    "size": "big"
                },
                "2_avg": {
                    "Type": "Blue",
                    "Key": "21",
                    "size": "big"
                }
            }
        }
    },
    "Value2": {
        "Build": {
            "1": {
                "t_inst": {
                    "Type": "Grey",
                    "Key": "22",
                    "size": "small"
                },
                "1_avg": {
                    "Type": "Yellow",
                    "Key": "8",
                    "size": "big"
                },
                "2_avg": {
                    "Type": "Blue",
                    "Key": "21",
                    "size": "big"
                }
            }
        }
    }
}

I've searched several forums, but none I've seen have as many layers as I need.

Could someone help me?

Edited by Leandro Feder
Posted (edited)

I create a VI to do that, but i'm not getting success
 


My actual structure is on this formact:
{"EQ1": { "TCP": { "1": {}, {"EQ1": { "TCP": { "2": {}, {"EQ1": { "TCP": { "3"{} } }, "EQ2": { "TCP": { "1":{}, "EQ2": { "TCP": { "2":{}, "EQ2": { "TCP": { "3":{} } }


I'm trying to do the folowing thing (the correct structure for json):

{"EQ1": { "TCP": { "1": {}, "2": {}, "3"{} } }, "EQ2": { "TCP": { "1":{}, "2":{}, "3":{} } }

 

Could you help me assemble my json into this structure?

BuildJSONtoForum.vi

Edited by Leandro Feder
  • 2 weeks later...
Posted

The examples you provide are invalid JSON, which makes it difficult to understand what you are actually trying to do.

In your VI, the input data is a 2D array of string but the JSON output is completely different. Your first step should be to define the types you need to produce the expected JSON output. Afterwards you can map your input data to the output data and simply convert it to JSON.

The structure of the inner-most object in your JSON appears to be the following:

{
  "Type":"ABC",
  "IP":"192.168.0.0",
  "Port":111,
  "Still":1,
  "Register":"Register",
  "Address":12345,
  "SizeLength":1,
  "FET":2,
  "Size":"big",
  "Conversion":"small"
}

In LabVIEW, this can be represented by a cluster:

image.png.47695f3cb674cd296dbc6ff214cb345c.png

When you convert this cluster to JSON, you'll get the output above.

Now, the next level of your structure is a bit strange but can be solved in a similar manner. I assume that "1", "2", and "3" are instances of the object above:

{
    "1": {},
    "2": {},
    "3": {}
}

So essentially, this is a cluster containing clusters:

image.png.b77745180b08477dbbca70017104a3be.png

The approach for the next level is practically the same:

{
    "TCP": {}
}

image.png.ef7f5db459239de4f3b35b6650faab93.png

And finally, there can be multiple instances of that, which, again, works the same:

{
    "EQ1": {},
    "EQ2": {}
}

image.png.f47946926340ff6c58df351eb482218e.png

This is the final form as far as I can tell. Now you can use either JSONtext or LabVIEW's built-in Flatten To JSON function to convert it to JSON

image.png.13218118e2a15aabca179f0fc658343e.png

{"EQ1":{"TCP":{"1":{"Type":"ABC","IP":"192.168.0.0","Port":111,"Still":1,"Register":"Register","Address":12345,"SizeLength":1,"FET":2,"Size":"big","Conversion":"small"},"2":{"Type":"ABC","IP":"192.168.0.0","Port":111,"Still":1,"Register":"Register","Address":12345,"SizeLength":1,"FET":2,"Size":"big","Conversion":"small"},"3":{"Type":"ABC","IP":"192.168.0.0","Port":111,"Still":1,"Register":"Register","Address":12345,"SizeLength":1,"FET":2,"Size":"big","Conversion":"small"}}},"EQ2":{"TCP":{"1":{"Type":"ABC","IP":"192.168.0.0","Port":111,"Still":1,"Register":"Register","Address":12345,"SizeLength":1,"FET":2,"Size":"big","Conversion":"small"},"2":{"Type":"ABC","IP":"192.168.0.0","Port":111,"Still":1,"Register":"Register","Address":12345,"SizeLength":1,"FET":2,"Size":"big","Conversion":"small"},"3":{"Type":"ABC","IP":"192.168.0.0","Port":111,"Still":1,"Register":"Register","Address":12345,"SizeLength":1,"FET":2,"Size":"big","Conversion":"small"}}}}

The mapping of your input data should be straight forward.

  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.