0

I'm looking for a tool/standard that converts allows configurable translation of json from one format, to another.

For example, lets say I have a source json:

{
   "user": {
      "firstName": "bob", 
      "lastName" : "jones"
   }
}

And I wanted to be create creating json objects like:

{
   fName: "bob", 
   lName: "jones"
}

Then I might have a translation configuration like:

{
   mappings: [
      {
          from: "src.user.firstName",  
          to: "target.fName"
      }, 
      {
          from: "src.user.lastName",  
          to: "target.lName"
      }
   ]
}

What I'm really looking for is a standard that does this in multiple languages, but failing that, if you can suggest tools for Go and JavaScript.

Note also, that you might want to do things like:

  • Extract values from arrays

Maybe like:

{
    mappings: [
       {
           from: "src.user.favoriteColors[0]", 
           to: "target.userFirstFavouriteColor", 
       }
    }
}

  • Put values into arrays

eg. translating:

{
    userMap: {
       id1: {
           name: "bob"
       }, 
       id2: {
           name: "alice"
       }
    }
}

to:


   users: [
       {
           name: "bob"
       }, 
       {
           name: "alice"
       }   
    ]
}

With a configuration maybe like:

{
    mappings: [
       {
           from: "src.userMap.*", 
           to: "target.users[]", 
       }
    }
}

  • Combine multiple values into one

eg. Maybe like:

{
    mappings: [
       {
           from: ["src.user.fName", "src.user.lName"], 
           to: "target.fullName", 
           stringFn: "{0} {1}"
       }
    }
}

I'm basically thinking XSLT but for json.

Does such a standard exist?

1

1 Answer 1

1

I would ask this question on SO within the context of the jq command line tool.

Just tag your question with jq. There are some jq experts on SO who can transform anything from any structure to any structure, with ease.

I have bookmarked and used a lot of their jq snippets myself.

You can define your own transformations and put them into functions in your $HOME/.jq directory.

For example, this transforms JSON to csv format.

def tocsv($x):
    $x
    |(map(keys)
        |add
        |unique
        |sort
    ) as $cols
    |map(. as $row
        |$cols
        |map($row[.]|tostring)
    ) as $rows
    |$cols,$rows[]
    | @csv;

With some tweets exported as tweets.json from the Twitter API, call this as

 <tweets.json jq  -s  -S \
  '[.[] \| {name:  .user.name, sn: .user.screen_name, loc: .user.location, url:.user.url, desc:.user.description, _fol: .user.followers_count }]'  \
  | jq  -f -r to_csv.jq

Another option is the newly-released (July 2022) "Invisible XML", which is also a draft W3C Standard. I haven't used ixml, but maybe someone else has applied it on JSON data.

this is its pitchline, from the inVisibleXML Tutorial:

"We choose which representations of our data to use, JSON, CSV, XML, or whatever, depending on habit, convenience, or the context we want to use that data in.
[...]
Invisible XML (ixml) is a method for treating non-XML documents as if they were XML, enabling authors to write documents and data in a format they prefer while providing XML for processes that are more effective with XML content."

1
  • I think JQ is valid answer. One could always write a node utility package around it.
    – dwjohnston
    Jun 14, 2023 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.