-3

I am unable to access any field in the following json file (someconfig.js) which starts with

window.table = {
  config: getBootstrapConfig()
};

window.someConfig = {
  header: {
    rui: {
      iframeUri: "header/header.html",
      divId: "",
      initFnName: ""
    }
  },
....
...
....

What did I try?

jq .header someconfig.js
parse error: Invalid numeric literal at line 1, column 14

jq .window.someConfig someconfig.js

parse error: Invalid numeric literal at line 2, column 0

I am new to jq and have also tried

jq .['header'] someconfig.js
jq: error: syntax error, unexpected INVALID_CHARACTER (Windows cmd shell quoting issues?) at <top-level>, line 1:
.['header']
jq: 1 compile error

However I am not able to understand how to fetch fields ex: divId, I want to see "". I also want to know what command would substitute divId ="" to divId = "abcd"

beegee Assem
  • 79
  • 1
  • 3
  • 16

2 Answers2

2

If you can somehow extract the data component of the JavaScript, you can use a tool such as to convert the JSON-like data to JSON, and then use jq.

For example:

#!/bin/bash

function data {
  cat<<EOF
{
  header: {
    rui: {
      iframeUri: "header/header.html",
      divId: "",
      initFnName: ""
    }
  }
}
EOF
}

data | hjson -j | jq .header.rui.iframeUri

yields

"header/header.html"
peak
  • 105,803
  • 17
  • 152
  • 177
0

You could perhaps use JSON.stringify to create a valid JSON string for subsequent processing by jq.

For example, suppose my.js contains the following javascript code:

window={};

window.someConfig = {
  header: {
    rui: {
      iframeUri: "header/header.html",
      divId: "",
      initFnName: ""
    }
  }
};

print(JSON.stringify(window));

Then using spidermonkey's js:

js my.js | jq .someConfig.header.rui.iframeUri

yields:

"header/header.html"
peak
  • 105,803
  • 17
  • 152
  • 177