JSON Parser in VB.Net - Accessing Tagnames with Hyphen

Started by QPRJohn, May 27, 2014, 08:41:33 AM

Previous topic - Next topic

QPRJohn

From my previous thread I have used the following code to obtain data from various tags using JSON Parser.


    Dim account = JsonConvert.DeserializeObject(Of JCXMP2)(Trim(json))

            frmXMPMetadata.txtDescription.Text = account.Description
            frmXMPMetadata.txtHeadline.Text = account.Headline
            frmXMPMetadata.txtCopyright.Text = account.Copyright
            frmXMPMetadata.txtWriterEditor.Text = account.Writer_Editor



The 'account.Writer_Editor' returns a null value (last line of code), as the name tag in the JSON string is "Writer-Editor".

VB.Net will not allow properties to be created with a hyphen in the property name.

I've been looking at various JSON related issues in different forums, but none are specific to VB.Net.

Can anyone suggest a work around for this?

Thanks John

Phil Harvey

Hi John,

The problem is that a hyphen is valid in an ExifTool tag name and a JSON object name, but not in a VB.Net property name.  So the JSON stream can't be deserialized directly into a VB.net object unless the hypens are converted into something else.  I am surprised that the JsonConvert object doesn't have some way to handle this automatically.  I have checked the Microsoft documentation for this, and it doesn't look like it is very flexible, so the work-around would be to filter the JSON yourself beforehand to remove the hyphens.

The alternative would be to find a JSON parser that gives you access to the original JSON object using a more flexible interface.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn

Phil

As suggested I removed the hyphens from the string before it was passed to the JSON parser, it works but I'm concerned that if I want to write a modified string back to the image/xmp file that other data that had the hyphen in it will be incorrect and cause other problems?

Another issue I have come across is when the JSON Parser gets to an array like "Keywords" I get an error thrown saying "Error reading string. Unexpected token: StartArray. Path 'Keywords', line 331, position 16."

Again, I don't know how to handle this  :(

I'm sinking fast!

Phil Harvey

Sounds like you need a new JSON parser if it can't even handle arrays. 

If you can't find one, you could write it yourself.  JSON is simple.  The ExifTool JSON parser is only about 100 lines of code, and if I removed the unrelated overhead and comments it could come down to about 20-30 lines.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn


QPRJohn

I'm not sure I'd know where to start writing my own JSON Parser!

Found and adapted the code here to overcome the "Writer-Editor" tagname issue



            Dim deserializedProduct As Object = JsonConvert.DeserializeObject(Of Object)(json)

            obj = JsonConvert.DeserializeObject(json)
            Try
                frmXMPMetadata.txtWriterEditor.Text = obj.Item("Writer-Editor").ToString
            Catch ex As Exception
            End Try
            Try
                frmXMPMetadata.txtDescription.Text = obj.Item("Description")
            Catch ex As Exception
            End Try
            Try
                frmXMPMetadata.txtHeadline.Text = obj.Item("Keywords")
            Catch ex As Exception
            End Try




I still had to remove the [] from each end of the string. It still throws the "array" exception with keywords. I guess I'll have to find a way of pulling arrays separately?

Once again thanks

Phil Harvey

I just had a thought:  If the arrays are your only problem, you can solve this by adding a -sep option to your command.  Maybe -sep ";" or something like that.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn

Phil

I added "-sep ;" to the command line. It got the array with no spaces between the words, added a bit of code to replace the ";" with "; " it works fine with the keywords tag! :D

Lets hope it will do the same with the rest!

The next challenge is writing them back as an array!  :o

Thank you very much!

I've been close to throwing in the towel!

John

Phil Harvey

Hi John,

To write them back as an array, just use the same -sep option as when you extracted them.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn

Phil

I'll report back when I get to that stage.

No doubt there will be other problems!

Thanks

John

QPRJohn

I'm back again!

I've tried several JSON forums etc; to attempt to use the "JsonConvert.SerializeObject" function to write back to a file without success. All the samples use "property" items and as previously discussed hyphens cannot be used in VB.Net properties! (Any ideas?)  :'(

I've reached a bit of "writers block" I've tried to find answers to my issues and seem to be going round in circles!

I thought I'd try by using exiftool's command line arguments to write back the relevant tags, can you tell me where to find out how to write an array to the dc:subject tag.

My attempts so far will only produce the various words as one string and not an array?

Thanks John


Phil Harvey

Hi John,

Quote from: QPRJohn on May 31, 2014, 06:45:01 AM
All the samples use "property" items and as previously discussed hyphens cannot be used in VB.Net properties! (Any ideas?)  :'(

Sorry, no.

QuoteI thought I'd try by using exiftool's command line arguments to write back the relevant tags, can you tell me where to find out how to write an array to the dc:subject tag.

My attempts so far will only produce the various words as one string and not an array?

Yes.  Use the -sep option as I mentioned in my last post (and read FAQ 17).

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn

Thanks Phil

Sorry to ask a question that was in the FAQ's, I had reached the stage where I couldn't see the wood for the trees!

Applied FAQ 17, and of course it works.

If it is of any interest I'll post any findings (if any) regarding the JSON issue.

John