Help with converting json to xml

Hi, I need help with converting the Afrikaans Bible from json to xml. The json structure looks to be weird (but I’m no specialist :smile:)
The book name are at the end of the verses. This is Matthew 1:1

[{Books:[{Chapters:[{Verses:[{
ID:1,Books:[{
ID:39,Chapters:[{Verses:[{
Text:"Hier is ’n lys van die name van Jesus Christus se voorouers. Jesus was die Seun van Dawid, en die Seun van Abraham."},{ID:2,

Thanks

What programming language are you wanting to use to do the conversion?

Hi umfundise welcome to the forum

I find it helpful to indent code, maybe you will too. eg.

[{Books:
    [{Chapters:
        [{Verses:
            [{ID:1,
              Books:
                [{ID:39,
                  Chapters:
                    [{Verses:
                        [{Text:"Hier is ’n lys van die name van Jesus Christus se voorouers. Jesus was die Seun van Dawid, en die Seun van Abraham."},
                         {ID:2, 

would have XML like

<Books>
  <Chapters>
    <Verses>
      <ID>1</ID>
      <Books>
        <ID>39</ID>
        <Chapters>
          <Verses>
            <Text>Hier is ... </Text>
            <ID>2</ID>

Obviously you’re going to want to not do this manually. You’re going to want to find something that will do this for you or put together some script that will do it.

1 Like

Thanks Mittineague! I’ll format my document accordingly. As you said, I would’t want to do this manually. Is their a regex string or parser which could assist? I’ve tried a regex string, but that’s when I realised that the original document doesn’t follow the standard throughout the document.

I don’t think I would try to use regex to parse the JSON string.

I looked to see if there was a native PHP XML function that accepted a multidimensional array of objects and arrays that returned XML that I was unaware of, but all I found was code put together by others.

My approach would be to decode the JSON string into an array / object. Iterate through it, and use XML functions to build the XML. Even if I did not end up using someone else’s code that I found in a search, I would certainly look at others code so I wouldn’t need to “reinvent” the entire “wheel”.

Of course there’s a very good possibility that a language other than PHP could do this much more easily. eg. Java, Perl, Python, Ruby, etc. So it could be worth your time to look for solutions in other languages if you have them available.

1 Like

Yes, in php it’s very easy to decode json to to an object or array.
Then there are extensions for working with xml, though it’s not something I’ve worked with, I would think it’s not too difficult to covert an object/array to xml with those.

I have some limited experience using PHP XML functions and IMHO once over the initial learning curve it’s fairly smooth sailing. (dealing with namespaces being an exception).

I think what would be the biggest hurdle for me would be putting together a recursive interator with the correct logic.

I’m also thinking that for something as large as the Bible there’s a better than good chance there will be memory and timeout issues. So breaking things down to “per book” would likely be a good idea even if it wouldn’t be a necessity.

In fact, I’m certain that during develop I would limit the work to a smaller subset of data instead of trying to work with it in it’s entirety. And then, after I was sure everything worked, then, maybe, I would try having it work with larger data sets.

I’m also wondering if XML is a better choice than database tables.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.