Jump to content

JSON Editor / Checker


Teken

Recommended Posts

Posted

I’m looking for a bullet proof application that will check JSON scripting / line code. I’ve already tried the on line LINT checker.

 

Absolutely garbage and ultimate turd!

 

Given I know nothing about JSON one would figure in 2020 if a site offers a easy and clean method to check code. It would work and be 100% accurate in its findings?!?

 

Nope . . .

 

How the night unfolded:

 

I must have spent the last 4 hours editing, and trying dozens of variations of code - nothing worked! After what seemed like days I looked up a few suggestions via Google for software which by the way didn’t offer very much.

 

Broke down and decided to use LINT only because a trusted friend indicated this would be helpful. So I tried again and the blasted site kept on saying line 1 was in error?!?

 

This is where probably two hours just got eaten up!! Why would I ever think this website is steering me wrong?!? Of corse having tried 100’s of combinations said to myself. Teken my boy something is rotten in Denmark

 

So I paste in working code and you guessed it. The freaking LINT tool says there are errors?!?

 

Someone throw a rope out for this drowning man - please!

 

 

Sent from my iPhone using Tapatalk

Posted

Is there something wrong with this JSON file??

{
    module: 'MMM-MQTT',
    position: 'bottom_left',
    header: 'MQTT',
    config: {
        logging: false,
        useWildcards: false,
        mqttServers: [
            {
                address: 'localhost',  // Server address or IP address
                port: '1883',          // Port number if other than default
                user: 'user',          // Leave out for no user
                password: 'password',  // Leave out for no password
                subscriptions: [
                    {
                        topic: 'smoky/1/inside/temperature', // Topic to look for
                        label: 'Temperature', // Displayed in front of value
                        suffix: '°C',         // Displayed after the value
                        decimals: 1,          // Round numbers to this number of decimals
                        sortOrder: 10,        // Can be used to sort entries in the same table
                        maxAgeSeconds: 60,    // Reduce intensity if value is older
                        colors: [             // Value dependent colors
                            { upTo: -10, value: "blue", label: "blue", suffix: "blue" },
                            { upTo: 0, value: "#00ccff", label: "#00ccff", suffix: "#00ccff" },
                            { upTo: 10, value: "yellow"},
                            { upTo: 25, label: "green", suffix: "green" },
                            { upTo: 100, label: "red" }, // The last one is used for higher values too
                        ],
                    },
                    {
                        topic: 'smoky/1/inside/humidity',
                        label: 'Luftfuktighet',
                        suffix: '%',
                        decimals: 0,
                        sortOrder: 20,
                        maxAgeSeconds: 60
                    },
                    {
                        topic: 'smoky/1/inside/smoke',
                        label: 'Røyk',
                        sortOrder: 30,
                        divide: 10, // Divide numeric values. Alternatively use `multiply`.
                        maxAgeSeconds: 60
                    },
                    {
                        topic: 'guests',
                        label: 'First guest',
                        jsonpointer: '/people/0/name'
                    },
                    {
                        topic: "house/1/doors/1",
                        label: "Door",
                        conversions: [
                            { from: "true", to: "Open" },
                            { from: "false", to: "Closed" }
                        ]
                    }
                ]
            }
        ],
    }
}

 

Posted
19 minutes ago, simplextech said:

Next time post it in a code block to make it easy to copy and also prevent the forum from replacing characters.

Use this to verify it:

https://codebeautify.org/jsonviewer

My quick check shows errors but may be from forum changes from how it was posted.

If you scroll down this is where the example was taken from: https://github.com/ottopaulsen/MMM-MQTT I've literally tried five different application and four web sites. No matter what *Known good working code* I enter they all declare an error?!?

So, it begs to ask the question for the dumb guys like me who have zero reference as to good, bad, ugly, vs working. How would I ever identify a real error in the line code if none of these tools even work? 

Posted
23 minutes ago, Teken said:

If you scroll down this is where the example was taken from: https://github.com/ottopaulsen/MMM-MQTT I've literally tried five different application and four web sites. No matter what *Known good working code* I enter they all declare an error?!?

Every site is declaring an error because the sample on that site is not valid JSON per W3C spec.  So all of the validators out there will have issues with it.

 

Posted
{"number":123,"string":"test"}

Property names must be double-quoted in valid JSON. On first glance, your sample doesn't.

Posted
9 minutes ago, jfailenschmid said:

{"number":123,"string":"test"}

Property names must be double-quoted in valid JSON. On first glance, your sample doesn't.

So here is what I find odd and greatly appreciate your input. I literally have working code that doesn't use double quotes?!? Why does that work and why do some JSON code require vs not require?

Also, does it matter where the } is placed?

Case in point:

}

} <- Does this matter where it is in the line ?

vs

       }

} <- As you can see I simply moved it over but its on the same line

Also, as I understand the { is the start or beginning of the code where as a }, indicates this is the end of the code.

When and why would does the } ] come in? is this a way to indicate a new entry / separate process / look at me??  

Posted
46 minutes ago, simplextech said:

Every site is declaring an error because the sample on that site is not valid JSON per W3C spec.  So all of the validators out there will have issues with it.

 

Yet, this and similar code will simply work or wont! Oh and the novice is supposed to know *The Why*?? 

Posted

Whitespace is ignored by the JSON parser, i.e., newline characters and spaces are insignificant.

If you have Node installed, you can open a shell window and type 'node' (without the quotes).

Then you can use the JSON global to check some sample string,

node
Welcome to Node.js v12.13.1.
Type ".help" for more information.
> JSON.parse('{"number":   -1\n   }')
{ number: -1 }
>
(To exit, press ^C again or ^D or type .exit)
>

 

Posted
5 hours ago, Teken said:

So here is what I find odd and greatly appreciate your input. I literally have working code that doesn't use double quotes?!? Why does that work and why do some JSON code require vs not require?

JSON isn't code, it's a standard for data notation, also sometimes called data serialization.

Whether or not your 'code' in question can deal with non-standard JSON, depends on the 'code' you're using to parse the data, hence YMMV. The truth is out there: JSON.parse() and JSON.stringify()

Posted
3 minutes ago, Teken said:

 


I freaking knew it - job security! emoji1785.pngemoji23.pngemoji2957.png


Sent from my iPhone using Tapatalk

 

Here's some "light" reading for you :)

https://en.wikipedia.org/wiki/JSON

JSON like other notations, descriptors, markups are not a language that runs they are simply a means to structure semi-human readable information for machine interpretation and thus the interpreter being used could define it's own variant of such markup.  Look at how broken HTML rendering has been over the years with different browsers.....

Another popular markup is YAML... that one is fun because of the static typed nature so if a space is out of whack it throws the whole thing off... :)

 

Posted
7 minutes ago, Goose66 said:

Did you figure it out? I think it's the "  ]," in the third to last line. This game is fun! ?

I don't know yet but will surely try all the suggestions from everyone here and elsewhere! I literally asked two *Real Programmer* and neither of them said what they see is wrong. I simply just don't know how anyone on the *Maker* world is expected to get ahead!

This used to be fun now its like work . . . 

Posted
1 minute ago, Teken said:

I don't know yet but will surely try all the suggestions from everyone here and elsewhere! I literally asked two *Real Programmer* and neither of them said what they see is wrong. I simply just don't know how anyone on the *Maker* world is expected to get ahead!

This used to be fun now its like work . . . 

Your original posted file has an error in literally every line except a handful. Do you have an update file? I can fix it for you.

Posted
25 minutes ago, firstone said:

Your original posted file has an error in literally every line except a handful. Do you have an update file? I can fix it for you.

Pretend I'm three and I don't know what you're asking? :D You want a copy of the file? The one I linked which has a hyperlinc is perfect example. But, you're saying there are errors in that persons written code?

Can you call out a few areas of concern for my review? Below is the short version of the *code* I was provided which is an edited version of what I posted here. Let me know how it would differ and will surely try the same once I return home:

{
    module: 'MMM-MQTT',
    position: 'bottom_left',
    header: 'MQTT',
    config: {
        logging: false,
        useWildcards: false,
        mqttServers: [
            {
                address: 'localhost',  // Server address or IP address
                port: '1883',          // Port number if other than default
                user: 'user',          // Leave out for no user
                password: 'password',  // Leave out for no password
                subscriptions: [
                    {
                        topic: 'dashbox/01000012/c1/watt',
                        label: 'Main Panel Watts',
                        suffix: 'W',
                        decimals: 0,
                        sortOrder: 20,  // Not sure what this does
                        maxAgeSeconds: 60
                    }
                        ]
                    }
                ]
            }
        ],
    }
}

EDIT: I believe the 60 should have a comma 60,

 

Posted
1 minute ago, Teken said:

Pretend I'm three and I don't know what you're asking? :D You want a copy of the file? The one I linked which has a hyperlinc is perfect example. But, you're saying there are errors in that persons written code?

Can you call out a few areas of concern for my review? Below is the short version of the *code* I was provided which is an edited version of what I posted here. Let me know how it would differ and will surely try the same once I return home:

{
    module: 'MMM-MQTT',
    position: 'bottom_left',
    header: 'MQTT',
    config: {
        logging: false,
        useWildcards: false,
        mqttServers: [
            {
                address: 'localhost',  // Server address or IP address
                port: '1883',          // Port number if other than default
                user: 'user',          // Leave out for no user
                password: 'password',  // Leave out for no password
                subscriptions: [
                    {
                        topic: 'dashbox/01000012/c1/watt',
                        label: 'Main Panel Watts',
                        suffix: 'W',
                        decimals: 0,
                        sortOrder: 20,  // Not sure what this does
                        maxAgeSeconds: 60
                    }
                        ]
                    }
                ]
            }
        ],
    }
}
 

Every "key" needs to be in double quotes. Every string value needs to be in double quotes. Comments are illegal. No extraneous commas (in the line 2 before last). Like people above said, some parsers aren't too strict so they might let it through. 

 

{
    "module": "MMM-MQTT",
    "position": "bottom_left",
    "header": "MQTT",
    "config": {
        "logging": false,
        "useWildcards": false,
        "mqttServers": [
            {
                "address": "localhost",
                "port": "1883",
 

Though the last line could be 

"port": 1883,

If it needs to be a number.

Posted

You can actually load your file as yaml, which is a lot more forgiving. The only issue is comment, which should be # instead of //.

Here's python code:

 

import json
import yaml

with open('test.yaml', 'r') as raw_data:
    data = yaml.safe_load(raw_data)
    print(json.dumps(data, indent=4))

 

Here's your json file:

{
    "module": "MMM-MQTT",
    "position": "bottom_left",
    "header": "MQTT",
    "config": {
        "logging": false,
        "useWildcards": false,
        "mqttServers": [
            {
                "address": "localhost",
                "port": "1883",
                "user": "user",
                "password": "password",
                "subscriptions": [
                    {
                        "topic": "smoky/1/inside/temperature",
                        "label": "Temperature",
                        "suffix": "\u00b0C",
                        "decimals": 1,
                        "sortOrder": 10,
                        "maxAgeSeconds": 60,
                        "colors": [
                            {
                                "upTo": -10,
                                "value": "blue",
                                "label": "blue",
                                "suffix": "blue"
                            },
                            {
                                "upTo": 0,
                                "value": "#00ccff",
                                "label": "#00ccff",
                                "suffix": "#00ccff"
                            },
                            {
                                "upTo": 10,
                                "value": "yellow"
                            },
                            {
                                "upTo": 25,
                                "label": "green",
                                "suffix": "green"
                            },
                            {
                                "upTo": 100,
                                "label": "red"
                            }
                        ]
                    },
                    {
                        "topic": "smoky/1/inside/humidity",
                        "label": "Luftfuktighet",
                        "suffix": "%",
                        "decimals": 0,
                        "sortOrder": 20,
                        "maxAgeSeconds": 60
                    },
                    {
                        "topic": "smoky/1/inside/smoke",
                        "label": "R\u00f8yk",
                        "sortOrder": 30,
                        "divide": 10,
                        "maxAgeSeconds": 60
                    },
                    {
                        "topic": "guests",
                        "label": "First guest",
                        "jsonpointer": "/people/0/name"
                    },
                    {
                        "topic": "house/1/doors/1",
                        "label": "Door",
                        "conversions": [
                            {
                                "from": "true",
                                "to": "Open"
                            },
                            {
                                "from": "false",
                                "to": "Closed"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

 

Posted
41 minutes ago, firstone said:

Every "key" needs to be in double quotes. Every string value needs to be in double quotes. Comments are illegal. No extraneous commas (in the line 2 before last). Like people above said, some parsers aren't too strict so they might let it through. 

 


{
    "module": "MMM-MQTT",
    "position": "bottom_left",
    "header": "MQTT",
    "config": {
        "logging": false,
        "useWildcards": false,
        "mqttServers": [
            {
                "address": "localhost",
                "port": "1883",
 

Though the last line could be 


"port": 1883,

If it needs to be a number.

@firstone

Once I return home will try this out and relay my findings. As always, I thank you and everyone who has taken the time to offer their insight and guidance!

Posted

For my own edification what does this mean exactly this is the last portion of the code at the very top:

  ] <- Why
                    } <- Why
                ] <- Why
            } <- Why
        ], <- Why didn't just stop there? Isn't this the end??
    } <- Why - does this even serve a purpose?
}  <- Why - Yet another french brace WTF??? 

Posted
27 minutes ago, Teken said:

For my own edification what does this mean exactly this is the last portion of the code at the very top:

  ] <- Why
                    } <- Why
                ] <- Why
            } <- Why
        ], <- Why didn't just stop there? Isn't this the end??
    } <- Why - does this even serve a purpose?
}  <- Why - Yet another french brace WTF??? 

So there are several types of data you can store in json (or any other format, for that matter).
 

  1. Scalar
    1. String: "key1": "value1"
    2. Numeric: "key2": 123
    3. Boolean: "key3": true
  2. List: 
    "listOfValues": [
    	"value1",
    	"value2",
    	"value3"
    ]

     

  3. Object. Object includes any of the above, including another object in brackets {}
    "someObject": {
    	"key1": "value1",
    	"key2": 123,
    	"someList": [
    		1,
    		2,
    		3
    	],
    	"subObject": {
    		"subKey1": "value3"
    	}
    }

So, basically, any time you open a list or an object, you normally offset indentation to make it easier to read, and once that's done, it needs to be closed. That's why you see all those ] and }.

That's how the parser knows to stop reading particular entity.

Now if you look at yaml, it has much simpler syntax. It doesn't need to have any curly brackets or brackets, but it is indentation based.

Posted
1 hour ago, firstone said:

So there are several types of data you can store in json (or any other format, for that matter).
 

  1. Scalar
    1. String: "key1": "value1"
    2. Numeric: "key2": 123
    3. Boolean: "key3": true
  2. List: 
    
    "listOfValues": [
    	"value1",
    	"value2",
    	"value3"
    ]

     

  3. Object. Object includes any of the above, including another object in brackets {}
    
    "someObject": {
    	"key1": "value1",
    	"key2": 123,
    	"someList": [
    		1,
    		2,
    		3
    	],
    	"subObject": {
    		"subKey1": "value3"
    	}
    }

So, basically, any time you open a list or an object, you normally offset indentation to make it easier to read, and once that's done, it needs to be closed. That's why you see all those ] and }.

That's how the parser knows to stop reading particular entity.

Now if you look at yaml, it has much simpler syntax. It doesn't need to have any curly brackets or brackets, but it is indentation based.

@firstone

As always, I thank you for teaching this old dog. 

Posted

Just spent the last four hours trying every known combination, to no avail. I feel the live force slowly being sucked out of me!
I enjoy a good challenge like most people but having worked on this for seven straight days truly sucks, monkey balls.

I need to walk away and do something different and clear my head.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...